Skip to content

Commit c51b492

Browse files
committed
Add Solution.py for 0421.Maximum XOR of Two Numbers in an Array
1 parent 332e04e commit c51b492

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution:
2+
def findMaximumXOR(self, nums: List[int]) -> int:
3+
max = 0
4+
mask = 0
5+
for i in range(30, -1, -1):
6+
current = 1 << i
7+
# 期望的二进制前缀
8+
mask = mask ^ current
9+
# 在当前前缀下, 数组内的前缀位数所有情况集合
10+
_set = set()
11+
for num in nums:
12+
_set.add(num & mask)
13+
# 期望最终异或值的从右数第i位为1, 再根据异或运算的特性推算假设是否成立
14+
flag = max | current
15+
for prefix in _set:
16+
if prefix ^ flag in _set:
17+
max = flag
18+
break
19+
return max

solution/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -768,7 +768,8 @@
768768
│   └── Solution.java
769769
├── 0421.Maximum XOR of Two Numbers in an Array
770770
│   ├── README.md
771-
│   └── Solution.java
771+
│   ├── Solution.java
772+
│   └── Solution.py
772773
├── 0423.Reconstruct Original Digits from English
773774
│   └── Solution.cpp
774775
├── 0427.Construct Quad Tree

0 commit comments

Comments
 (0)