@@ -20,35 +20,37 @@ Could you do this in O(n) runtime?
20
20
*/
21
21
public class _421 {
22
22
23
- //credit: https://discuss.leetcode.com/topic/63213/java-o-n-solution-using-bit-manipulation-and-hashmap/7
24
- public int findMaximumXOR (int [] nums ) {
25
- int max = 0 ;
26
- int mask = 0 ;
27
- for (int i = 31 ; i >= 0 ; i --) {
28
- mask |= (1 << i );//the mask will grow like this: 100...000, 110...000, 111...000 to 111...111, each time, we only get the most left part of all numbers in the given array
29
- System .out .println ("mask = " + Integer .toBinaryString (mask ));
30
- Set <Integer > set = new HashSet <>();
31
- for (int num : nums ) {
32
- System .out .println ("num = " + Integer .toBinaryString (num ));
33
- set .add (num & mask );
34
- System .out .println ("mask & num = " + Integer .toBinaryString (mask & num ));
35
- }
23
+ public static class Solution1 {
24
+ //credit: https://discuss.leetcode.com/topic/63213/java-o-n-solution-using-bit-manipulation-and-hashmap/7
25
+ public int findMaximumXOR (int [] nums ) {
26
+ int max = 0 ;
27
+ int mask = 0 ;
28
+ for (int i = 31 ; i >= 0 ; i --) {
29
+ mask |= (1 << i );//the mask will grow like this: 100...000, 110...000, 111...000 to 111...111, each time, we only get the most left part of all numbers in the given array
30
+ System .out .println ("mask = " + Integer .toBinaryString (mask ));
31
+ Set <Integer > set = new HashSet <>();
32
+ for (int num : nums ) {
33
+ System .out .println ("num = " + Integer .toBinaryString (num ));
34
+ set .add (num & mask );
35
+ System .out .println ("mask & num = " + Integer .toBinaryString (mask & num ));
36
+ }
36
37
37
- int candidate = max | (1 << i );
38
- System .out .println ("candidate = " + Integer .toBinaryString (candidate ));
39
- /**Reason behind this: if a ^ prefix = candidate, then a ^ candidate = prefix, also prefix ^ candidate = a
40
- * in this below code: we use this one: prefix ^ candidate = a*/
41
- for (int prefix : set ) {
42
- System .out .println ("candidate ^ prefix = " + Integer .toBinaryString (candidate ^ prefix ));
43
- if (set .contains (candidate ^ prefix )) {
44
- max = candidate ;
38
+ int candidate = max | (1 << i );
39
+ System .out .println ("candidate = " + Integer .toBinaryString (candidate ));
40
+ /**Reason behind this: if a ^ prefix = candidate, then a ^ candidate = prefix, also prefix ^ candidate = a
41
+ * in this below code: we use this one: prefix ^ candidate = a*/
42
+ for (int prefix : set ) {
43
+ System .out .println ("candidate ^ prefix = " + Integer .toBinaryString (candidate ^ prefix ));
44
+ if (set .contains (candidate ^ prefix )) {
45
+ max = candidate ;
46
+ }
45
47
}
48
+ System .out .println ("max = " + max );
49
+ System .out .println ("i = " + i );
50
+ System .out .println ("===============================================" );
46
51
}
47
- System .out .println ("max = " + max );
48
- System .out .println ("i = " + i );
49
- System .out .println ("===============================================" );
52
+ return max ;
50
53
}
51
- return max ;
52
54
}
53
55
54
56
}
0 commit comments