-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path421.cpp
More file actions
executable file
·107 lines (106 loc) · 3.07 KB
/
421.cpp
File metadata and controls
executable file
·107 lines (106 loc) · 3.07 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <bits/stdc++.h>
#include <algorithm>
using namespace std;
class Solution {
public:
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode() : val(0), left(nullptr), right(nullptr) {}
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
int maxLengthValue;
TreeNode* buildTrie(vector<string> binarys){
//left 0 right 1
TreeNode* head = new TreeNode(-1);
for (auto binary : binarys){
TreeNode* tmp = head;
for (auto c : binary){
if (c == '0'){
if (tmp->left){
tmp = tmp->left;
}else{
tmp->left = new TreeNode('0');
tmp = tmp->left;
}
}else{
if (tmp->right){
tmp = tmp->right;
}else{
tmp->right = new TreeNode('1');
tmp = tmp->right;
}
}
}
}
return head;
}
int calMaxXor(string binary, TreeNode* head){
long long ans = 0;
long long multiplier = 1 << (maxLengthValue - 1);
TreeNode* tmp = head;
for (auto c : binary){
//cout << c << endl;
if (c == '0'){
if (tmp->right){
ans += multiplier * 1;
multiplier /= 2;
tmp = tmp->right;
}else{
tmp = tmp->left;
}
}else{
if (tmp->left){
ans += multiplier * 1;
multiplier /= 2;
tmp = tmp->left;
}else{
tmp = tmp->right;
}
}
}
return ans;
}
int max(int a, int b){
return a >= b ? a : b;
}
string transfer(int orig){
string res = "";
while (orig > 0){
// 17 % 2 1; 8 % 2 0; 4 % 2 0; 2 % 2 0; 1 % 2 1; 10001 => 1*2^4 + 1
res += '0' + orig % 2;
orig /= 2;
}
return res;
}
int maxLength(vector<string> nums){
int res = INT_MIN;
for (string s: nums){
res = max(res, s.size());
}
return res;
}
int findMaximumXOR(vector<int>& nums) {
vector<string> binarys;
for (int num: nums){
binarys.push_back(transfer(num));
}
maxLengthValue = maxLength(binarys);
for (auto & binary : binarys){
binary = string(maxLengthValue - binary.size(),'0') + binary;
}
TreeNode* head = buildTrie(binarys);
int res = INT_MIN;
for (auto binary : binarys){
res = max(res,calMaxXor(binary,head));
}
return res;
}
};
int main(){
Solution s;
vector<int> test{3,10,5,25,2,8};
cout << s.findMaximumXOR(test)<< endl;
}