-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path421findMaximumXOR.py
85 lines (77 loc) · 2.18 KB
/
421findMaximumXOR.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
'''
构造前缀树
'''
class Solution:
def findMaximumXOR(self, nums: List[int]) -> int:
nodes = [[0,0]] # 用来存储每一个节点,每个节点有两个儿子0 和 1
# 先存储根节点
for x in nums:
p = 0 # p指向节点的下标
for i in range(30, -1, -1):
t = x >> i & 1 # t 取值 {0,1}
if not nodes[p][t]:
nodes.append([0,0])
nodes[p][t] = len(nodes)-1 # 指向刚刚插入的节点
p = nodes[p][t]
#end_for 将所有的数以二进制形式插入前缀树
res = 0
for x in nums:
p, xorv = 0, 0
for i in range(30, -1, -1):
t = x >> i & 1
if nodes[p][1-t]:
p = nodes[p][1-t]
xorv += 1 << i
else:
p = nodes[p][t]
#end_for
res = max(res, xorv)
#end_for
return res
##########################
############C++###########
##########################
class Solution {
public:
struct Node {
int son[2];
};
vector<Node> nodes;
int findMaximumXOR(vector<int>& nums) {
nodes.push_back(Node({0, 0}));
for (auto x : nums)
{
int p = 0;
for (int i=30; i>=0; i--)
{
int t = x >> i & 1;
if(!nodes[p].son[t])
{
nodes.push_back(Node({0, 0}));
nodes[p].son[t] = nodes.size() - 1;
}
p = nodes[p].son[t];
}
}
int res = 0;
for (auto x : nums)
{
int p=0, xorv = 0;
for (int i=30; i>=0; i--)
{
int t = x >> i & 1;
if (nodes[p].son[!t])
{
p = nodes[p].son[!t];
xorv += 1 << i;
}
else
{
p = nodes[p].son[t];
}
}
res = max(xorv, res);
}
return res;
}
};