Skip to content

Commit 80065f2

Browse files
author
whd
committed
upload
1 parent bd9fe12 commit 80065f2

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
class Solution {
2+
public:
3+
int findMaximumXOR(vector<int>& nums) {
4+
struct Node {
5+
shared_ptr<Node> c[2];
6+
};
7+
using ptr = shared_ptr<Node>;
8+
ptr root(new Node());
9+
auto insert = [&] (int num) {
10+
ptr now = root;
11+
for (int k = 30; k >= 0; k--) {
12+
auto c = num >> k & 1;
13+
if (!now->c[c]) {
14+
now->c[c] = ptr(new Node());
15+
}
16+
now = now->c[c];
17+
}
18+
};
19+
auto find = [&] (int num) {
20+
ptr now = root;
21+
int ans = 0;
22+
for (int k = 30; k >= 0; k--) {
23+
auto c = !(num >> k & 1);
24+
if (now->c[c]) {
25+
ans += 1 << k;
26+
} else {
27+
c = !c;
28+
}
29+
now = now->c[c];
30+
}
31+
return ans;
32+
};
33+
int ans = 0;
34+
for (int i = 0; i < nums.size(); i++) {
35+
if (i) {
36+
ans = max(ans, find(nums[i]));
37+
}
38+
insert(nums[i]);
39+
}
40+
return ans;
41+
}
42+
};

0 commit comments

Comments
 (0)