Skip to content

Commit 3df593d

Browse files
committed
0089. Gray Code
1 parent 0620b38 commit 3df593d

File tree

4 files changed

+147
-0
lines changed

4 files changed

+147
-0
lines changed

markdown/0089. Gray Code.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
### [89\. Gray Code](https://leetcode.com/problems/gray-code/)
2+
3+
Difficulty: **Medium**
4+
5+
6+
The gray code is a binary numeral system where two successive values differ in only one bit.
7+
8+
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.
9+
10+
**Example 1:**
11+
12+
```
13+
Input: 2
14+
Output: [0,1,3,2]
15+
Explanation:
16+
00 - 0
17+
01 - 1
18+
11 - 3
19+
10 - 2
20+
21+
For a given n, a gray code sequence may not be uniquely defined.
22+
For example, [0,2,3,1] is also a valid gray code sequence.
23+
24+
00 - 0
25+
10 - 2
26+
11 - 3
27+
01 - 1
28+
```
29+
30+
**Example 2:**
31+
32+
```
33+
Input: 0
34+
Output: [0]
35+
Explanation: We define the gray code sequence to begin with 0.
36+
  A gray code sequence of n has size = 2n, which for n = 0 the size is 20 = 1.
37+
  Therefore, for n = 0 the gray code sequence is [0].
38+
```
39+
40+
41+
#### Solution
42+
43+
Language: **Java**
44+
45+
```java
46+
class Solution {
47+
   public List<Integer> grayCode(int n) {
48+
       List<Integer> result = new ArrayList<>();
49+
       result.add(0);
50+
       for (int i = 0; i < n; i++) {
51+
           for (int j = result.size() - 1; j >= 0; j--) { // 从尾部开始,用于保持连续(即每次只差一个bit位)
52+
               result.add(result.get(j) | 1 << i);
53+
          }
54+
      }
55+
       return result;
56+
  }
57+
}
58+
```
59+
![pic](https://raw.githubusercontent.com/PicGoBed/PicBed/master/2019-08-01-RdPkMU.jpg)

src/main/java/leetcode/_89_/Main.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package leetcode._89_;
2+
3+
/**
4+
* Created by zhangbo54 on 2019-03-04.
5+
*/
6+
public class Main {
7+
public static void main(String[] args) {
8+
Solution solution = new Solution();
9+
System.out.println(solution.grayCode(2));
10+
}
11+
}
12+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package leetcode._89_;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
class Solution {
7+
public List<Integer> grayCode(int n) {
8+
List<Integer> result = new ArrayList<>();
9+
result.add(0);
10+
for (int i = 0; i < n; i++) {
11+
for (int j = result.size() - 1; j >= 0; j--) { // 从尾部开始,用于保持连续(即每次只差一个bit位)
12+
result.add(result.get(j) | 1 << i);
13+
}
14+
}
15+
return result;
16+
}
17+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
### [89\. Gray Code](https://leetcode.com/problems/gray-code/)
2+
3+
Difficulty: **Medium**
4+
5+
6+
The gray code is a binary numeral system where two successive values differ in only one bit.
7+
8+
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.
9+
10+
**Example 1:**
11+
12+
```
13+
Input: 2
14+
Output: [0,1,3,2]
15+
Explanation:
16+
00 - 0
17+
01 - 1
18+
11 - 3
19+
10 - 2
20+
21+
For a given n, a gray code sequence may not be uniquely defined.
22+
For example, [0,2,3,1] is also a valid gray code sequence.
23+
24+
00 - 0
25+
10 - 2
26+
11 - 3
27+
01 - 1
28+
```
29+
30+
**Example 2:**
31+
32+
```
33+
Input: 0
34+
Output: [0]
35+
Explanation: We define the gray code sequence to begin with 0.
36+
  A gray code sequence of n has size = 2n, which for n = 0 the size is 20 = 1.
37+
  Therefore, for n = 0 the gray code sequence is [0].
38+
```
39+
40+
41+
#### Solution
42+
43+
Language: **Java**
44+
45+
```java
46+
class Solution {
47+
   public List<Integer> grayCode(int n) {
48+
       List<Integer> result = new ArrayList<>();
49+
       result.add(0);
50+
       for (int i = 0; i < n; i++) {
51+
           for (int j = result.size() - 1; j >= 0; j--) { // 从尾部开始,用于保持连续(即每次只差一个bit位)
52+
               result.add(result.get(j) | 1 << i);
53+
          }
54+
      }
55+
       return result;
56+
  }
57+
}
58+
```
59+
![pic](https://raw.githubusercontent.com/PicGoBed/PicBed/master/2019-08-01-RdPkMU.jpg)

0 commit comments

Comments
 (0)