-
Notifications
You must be signed in to change notification settings - Fork 1
/
tree.js
61 lines (51 loc) · 1.3 KB
/
tree.js
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
/**
* @param {number[]} nums
* @return {number}
*/
var findMaximumXOR = function(nums) {
const tree = {};
let _num;
let nonius;
let i = 0;
let j;
let l = nums.length;
let numL;
let tempNum;
for (; i < l; ++i) {
_num = nums[i].toString(2);
numL = _num.length;
nonius = tree;
for (j = 0; 32 > j; ++j) {
if (32 - j > numL) {
tempNum = '0';
} else {
tempNum = _num[j + numL - 32];
}
if (!nonius[tempNum]) {
nonius[tempNum] = {};
}
nonius = nonius[tempNum];
}
nonius.value = nums[i];
}
let result = 0;
for (i = 0; i < l; ++i) {
_num = nums[i].toString(2);
numL = _num.length;
nonius = tree;
for (j = 0; 32 > j; ++j) {
if (nonius[0] && nonius[1]) {
if (32 - j > numL) {
tempNum = '0';
} else {
tempNum = _num[j + numL - 32];
}
nonius = '0' === tempNum ? nonius[1] : nonius[0];
} else {
nonius = nonius[0] || nonius[1];
}
}
result = Math.max(result, nonius.value ^ nums[i]);
}
return result;
};