Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 981ff48

Browse files
refactor 421
1 parent a38f368 commit 981ff48

File tree

2 files changed

+30
-31
lines changed

2 files changed

+30
-31
lines changed

src/main/java/com/fishercoder/solutions/_421.java

+27-25
Original file line numberDiff line numberDiff line change
@@ -20,35 +20,37 @@ Could you do this in O(n) runtime?
2020
*/
2121
public class _421 {
2222

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+
}
3637

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+
}
4547
}
48+
System.out.println("max = " + max);
49+
System.out.println("i = " + i);
50+
System.out.println("===============================================");
4651
}
47-
System.out.println("max = " + max);
48-
System.out.println("i = " + i);
49-
System.out.println("===============================================");
52+
return max;
5053
}
51-
return max;
5254
}
5355

5456
}

src/test/java/com/fishercoder/_421Test.java

+3-6
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,22 @@
66

77
import static junit.framework.Assert.assertEquals;
88

9-
/**
10-
* Created by fishercoder on 4/28/17.
11-
*/
129
public class _421Test {
13-
private static _421 test;
10+
private static _421.Solution1 solution1;
1411
private static int expected;
1512
private static int actual;
1613
private static int[] nums;
1714

1815
@BeforeClass
1916
public static void setup() {
20-
test = new _421();
17+
solution1 = new _421.Solution1();
2118
}
2219

2320
@Test
2421
public void test1() {
2522
nums = new int[]{3, 10, 5, 25, 2, 8};
2623
expected = 28;
27-
actual = test.findMaximumXOR(nums);
24+
actual = solution1.findMaximumXOR(nums);
2825
assertEquals(expected, actual);
2926
}
3027
}

0 commit comments

Comments
 (0)