|
3 | 3 | import java.util.ArrayList;
|
4 | 4 | import java.util.List;
|
5 | 5 |
|
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 |
| - |
30 | 6 | public class _89 {
|
31 | 7 |
|
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 | + } |
39 | 16 | }
|
40 |
| - } |
41 | 17 |
|
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 | + } |
49 | 26 | }
|
50 |
| - } |
51 | 27 |
|
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 | + } |
70 | 46 |
|
71 | 47 | }
|
0 commit comments