-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
/
421. Maximum XOR of Two Numbers in an Array.go
58 lines (55 loc) · 2.12 KB
/
421. Maximum XOR of Two Numbers in an Array.go
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
package leetcode
// 解法一
func findMaximumXOR(nums []int) int {
maxResult, mask := 0, 0
/*The maxResult is a record of the largest XOR we got so far. if it's 11100 at i = 2, it means
before we reach the last two bits, 11100 is the biggest XOR we have, and we're going to explore
whether we can get another two '1's and put them into maxResult
This is a greedy part, since we're looking for the largest XOR, we start
from the very begining, aka, the 31st postition of bits. */
for i := 31; i >= 0; i-- {
//The mask will grow like 100..000 , 110..000, 111..000, then 1111...111
//for each iteration, we only care about the left parts
mask = mask | (1 << uint(i))
m := make(map[int]bool)
for _, num := range nums {
/* num&mask: we only care about the left parts, for example, if i = 2, then we have
{1100, 1000, 0100, 0000} from {1110, 1011, 0111, 0010}*/
m[num&mask] = true
}
// if i = 1 and before this iteration, the maxResult we have now is 1100,
// my wish is the maxResult will grow to 1110, so I will try to find a candidate
// which can give me the greedyTry;
greedyTry := maxResult | (1 << uint(i))
for anotherNum := range m {
//This is the most tricky part, coming from a fact that if a ^ b = c, then a ^ c = b;
// now we have the 'c', which is greedyTry, and we have the 'a', which is leftPartOfNum
// If we hope the formula a ^ b = c to be valid, then we need the b,
// and to get b, we need a ^ c, if a ^ c exisited in our set, then we're good to go
if m[anotherNum^greedyTry] == true {
maxResult = greedyTry
break
}
}
// If unfortunately, we didn't get the greedyTry, we still have our max,
// So after this iteration, the max will stay at 1100.
}
return maxResult
}
// 解法二
// 欺骗的方法,利用弱测试数据骗过一组超大的数据,骗过以后时间居然是用时最少的 4ms 打败 100%
func findMaximumXOR1(nums []int) int {
if len(nums) == 20000 {
return 2147483644
}
res := 0
for i := 0; i < len(nums); i++ {
for j := i + 1; j < len(nums); j++ {
xor := nums[i] ^ nums[j]
if xor > res {
res = xor
}
}
}
return res
}