1442. 形成两个异或相等数组的三元组数目
为保证权益,题目请参考 1442. 形成两个异或相等数组的三元组数目(From LeetCode).
解决方案1
Python
python
from typing import List
class Solution:
def countTriplets(self, arr: List[int]) -> int:
setss = dict()
ans = 0
for i in range(len(arr)):
tmp = arr[i]
for j in range(i + 1, len(arr)):
tmp = tmp ^ arr[j]
if tmp in setss:
ans += setss[tmp]
setss[tmp] += 1
else:
setss[tmp] = 1
return ans
if __name__ == "__main__":
so = Solution()
print(so.countTriplets([1, 1, 1, 1, 1]))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22