Skip to content

Commit 24dbdf1

Browse files
refactor 89
1 parent 7eb902c commit 24dbdf1

File tree

1 file changed

+34
-58
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+34
-58
lines changed

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

Lines changed: 34 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -3,69 +3,45 @@
33
import java.util.ArrayList;
44
import java.util.List;
55

6-
/**
7-
* 89. Gray Code
8-
*
9-
* The gray code is a binary numeral system where two successive values differ in only one bit.
10-
* Given a non-negative integer n representing the total number of bits in the code,
11-
* print the sequence of gray code.
12-
* A gray code sequence must begin with 0.
13-
14-
For example, given n = 2, return [0,1,3,2]. Its gray code sequence is:
15-
16-
00 - 0
17-
01 - 1
18-
11 - 3
19-
10 - 2
20-
21-
Note:
22-
23-
For a given n, a gray code sequence is not uniquely defined.
24-
25-
For example, [0,2,3,1] is also a valid gray code sequence according to the above definition.
26-
27-
For now, the judge is able to judge based on one instance of gray code sequence. Sorry about that.
28-
*/
29-
306
public class _89 {
317

32-
public static class Solution1 {
33-
public List<Integer> grayCode(int n) {
34-
List<Integer> result = new ArrayList();
35-
for (int i = 0; i < (1 << n); i++) {
36-
result.add(i ^ (i >> 1));
37-
}
38-
return result;
8+
public static class Solution1 {
9+
public List<Integer> grayCode(int n) {
10+
List<Integer> result = new ArrayList();
11+
for (int i = 0; i < (1 << n); i++) {
12+
result.add(i ^ (i >> 1));
13+
}
14+
return result;
15+
}
3916
}
40-
}
4117

42-
public static class Solution2 {
43-
public List<Integer> grayCode(int n) {
44-
List<Integer> result = new ArrayList();
45-
for (int i = 0; i < Math.pow(2, n); i++) {
46-
result.add(i ^ (i >> 1));
47-
}
48-
return result;
18+
public static class Solution2 {
19+
public List<Integer> grayCode(int n) {
20+
List<Integer> result = new ArrayList();
21+
for (int i = 0; i < Math.pow(2, n); i++) {
22+
result.add(i ^ (i >> 1));
23+
}
24+
return result;
25+
}
4926
}
50-
}
5127

52-
public static void main(String... args) {
53-
System.out.println("-----------------------------------------------------------------------------------------");
54-
System.out.println("How to understand i << n? It means n to the power of two, see below. So we have an equivalent solution, which is solution2.");
55-
System.out.println("1 << 2: " + (1 << 2));
56-
System.out.println("1 << 3: " + (1 << 3));
57-
System.out.println("1 << 4: " + (1 << 4));
58-
System.out.println("1 << 5: " + (1 << 5));
59-
System.out.println("1 << 6: " + (1 << 6));
60-
System.out.println("-----------------------------------------------------------------------------------------");
61-
System.out.println("How to understand i >> 1? It means to shift the number i to the right by 1 bit, see below");
62-
System.out.println("0 >> 1: " + (0 >> 1));
63-
System.out.println("1 >> 1: " + (1 >> 1));
64-
System.out.println("2 >> 1: " + (2 >> 1));
65-
System.out.println("3 >> 1: " + (3 >> 1));
66-
System.out.println("4 >> 1: " + (4 >> 1));
67-
System.out.println("5 >> 1: " + (5 >> 1));
68-
System.out.println("6 >> 1: " + (6 >> 1));
69-
}
28+
public static void main(String... args) {
29+
System.out.println("-----------------------------------------------------------------------------------------");
30+
System.out.println("How to understand i << n? It means n to the power of two, see below. So we have an equivalent solution, which is solution2.");
31+
System.out.println("1 << 2: " + (1 << 2));
32+
System.out.println("1 << 3: " + (1 << 3));
33+
System.out.println("1 << 4: " + (1 << 4));
34+
System.out.println("1 << 5: " + (1 << 5));
35+
System.out.println("1 << 6: " + (1 << 6));
36+
System.out.println("-----------------------------------------------------------------------------------------");
37+
System.out.println("How to understand i >> 1? It means to shift the number i to the right by 1 bit, see below");
38+
System.out.println("0 >> 1: " + (0 >> 1));
39+
System.out.println("1 >> 1: " + (1 >> 1));
40+
System.out.println("2 >> 1: " + (2 >> 1));
41+
System.out.println("3 >> 1: " + (3 >> 1));
42+
System.out.println("4 >> 1: " + (4 >> 1));
43+
System.out.println("5 >> 1: " + (5 >> 1));
44+
System.out.println("6 >> 1: " + (6 >> 1));
45+
}
7046

7147
}

0 commit comments

Comments
 (0)