421:数组中两个数的最大异或值
This commit is contained in:
parent
21d58217d4
commit
bc935f1921
@ -71,25 +71,23 @@ public class MaximumXorOfTwoNumbersInAnArray {
|
|||||||
//leetcode submit region begin(Prohibit modification and deletion)
|
//leetcode submit region begin(Prohibit modification and deletion)
|
||||||
class Solution {
|
class Solution {
|
||||||
public int findMaximumXOR(int[] nums) {
|
public int findMaximumXOR(int[] nums) {
|
||||||
int x = 0;
|
int result = 0;
|
||||||
for (int k = 30; k >= 0; k--) {
|
for (int i = 30; i >= 0; i--) {
|
||||||
Set<Integer> set = new HashSet<>();
|
Set<Integer> set = new HashSet<>();
|
||||||
boolean found = false;
|
boolean isHave = false;
|
||||||
|
|
||||||
for (int num : nums) {
|
for (int num : nums) {
|
||||||
set.add(num >> k);
|
set.add(num >> i);
|
||||||
if (set.contains(((x << 1) + 1) ^ (num >> k))) {
|
if (set.contains(((result << 1) + 1) ^ (num >> i))) {
|
||||||
found = true;
|
isHave = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
result <<= 1;
|
||||||
x <<= 1;
|
if (isHave) {
|
||||||
if (found) {
|
result++;
|
||||||
x++;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return x;
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//leetcode submit region end(Prohibit modification and deletion)
|
//leetcode submit region end(Prohibit modification and deletion)
|
||||||
|
Loading…
Reference in New Issue
Block a user