File tree Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Original file line number Diff line number Diff line change
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
+ };
You can’t perform that action at this time.
0 commit comments