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

Commit b16de1a

Browse files
refactor 89
1 parent e4ec942 commit b16de1a

File tree

1 file changed

+14
-2
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+14
-2
lines changed

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
11
package com.fishercoder.solutions;
22

3+
import com.fishercoder.common.utils.CommonUtils;
4+
35
import java.util.ArrayList;
46
import java.util.List;
57

68
/**
79
* 89. Gray Code
810
*
911
* The gray code is a binary numeral system where two successive values differ in only one bit.
10-
11-
Given a non-negative integer n representing the total number of bits in the code, print the sequence of gray code. A gray code sequence must begin with 0.
12+
* Given a non-negative integer n representing the total number of bits in the code,
13+
* print the sequence of gray code.
14+
* A gray code sequence must begin with 0.
1215
1316
For example, given n = 2, return [0,1,3,2]. Its gray code sequence is:
1417
1518
00 - 0
1619
01 - 1
1720
11 - 3
1821
10 - 2
22+
1923
Note:
24+
2025
For a given n, a gray code sequence is not uniquely defined.
2126
2227
For example, [0,2,3,1] is also a valid gray code sequence according to the above definition.
@@ -34,4 +39,11 @@ public List<Integer> grayCode(int n) {
3439
return result;
3540
}
3641

42+
public static void main(String... args) {
43+
int n = 3;
44+
System.out.println("1 << n = " + (1 << n));
45+
_89 test = new _89();
46+
List<Integer> result = test.grayCode(n);
47+
CommonUtils.printList(result);
48+
}
3749
}

0 commit comments

Comments
 (0)