题目:
89. 格雷编码
题解:
/**
关键是搞清楚格雷编码的生成过程, G(i) = i ^ (i/2);
如 n = 3:
G(0) = 000,
G(1) = 1 ^ 0 = 001 ^ 000 = 001
G(2) = 2 ^ 1 = 010 ^ 001 = 011
G(3) = 3 ^ 1 = 011 ^ 001 = 010
G(4) = 4 ^ 2 = 100 ^ 010 = 110
G(5) = 5 ^ 2 = 101 ^ 010 = 111
G(6) = 6 ^ 3 = 110 ^ 011 = 101
G(7) = 7 ^ 3 = 111 ^ 011 = 100
**/
代码:
import java.util.*;
public class code89 {
public static List<Integer> grayCode(int n) {
List<Integer> ret = new ArrayList<>();
for (int i = 0; i < 1 << n; ++i) {
ret.add(i ^ i >> 1);
}
return ret;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNextInt()) {
int n = sc.nextInt();
List<Integer> ans = grayCode(n);
System.out.println(ans);
}
}
}
参考:
- 格雷码 - 维基百科
- Gray Code (镜像反射法,图解)
- 详细通俗的思路分析,多解法
- 逐步推进的动态规划解法
- 根据格雷码的性质