/
_0089_GrayCode.java
39 lines (36 loc) · 1.24 KB
/
_0089_GrayCode.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.diguage.algorithm.leetcode;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* = 89. Gray Code
*
* https://leetcode.com/problems/gray-code/[Gray Code - LeetCode]
*
* @author D瓜哥, https://www.diguage.com/
* @since 2020-02-05 19:36
*/
public class _0089_GrayCode {
/**
* Runtime: 0 ms, faster than 100.00% of Java online submissions for Gray Code.
* Memory Usage: 37 MB, less than 8.00% of Java online submissions for Gray Code.
*
* Copy from: https://leetcode-cn.com/problems/gray-code/solution/gray-code-jing-xiang-fan-she-fa-by-jyd/[Gray Code (镜像反射法,图解) - 格雷编码 - 力扣(LeetCode)]
*/
public List<Integer> grayCode(int n) {
List<Integer> result = new ArrayList<>((int) Math.pow(2, n));
result.add(0);
int head = 1;
for (int i = 0; i < n; i++) {
for (int j = result.size() - 1; j >= 0; j--) {
result.add(head + result.get(j));
}
head <<= 1;
}
return result;
}
public static void main(String[] args) {
_0089_GrayCode solution = new _0089_GrayCode();
System.out.println(Arrays.toString(solution.grayCode(4).toArray()));
}
}