Problem
The gray code is a binary numeral system where two successive values differ in only one bit.
Given a non-negative integer n representing the total number of bits in the code, find the sequence of gray code. A gray code sequence must begin with 0 and with cover all 2n integers.
http://mathworld.wolfram.com/...
Example
Given n = 2, return [0,1,3,2]. Its gray code sequence is:
00 - 0
01 - 1
11 - 3
10 - 2
Solution
其实就是找规律,0-01-0132-01326754这样,每个循环(i):
增加当前res.size()个新数;
新的循环先在2进制的第(i+1)位加1,同时与之前res所存的数字倒序相加产生新数;
存入新数,进入下一个循环后更新size。
参考了codesolutiony的算法,简化了一下:
public class Solution {
public ArrayList<Integer> grayCode(int n) {
ArrayList<Integer> res = new ArrayList<Integer>();
res.add(0);
for (int i = 0; i < n; i++) {
//每个循环更新size: 1, 3, 7...
int size = res.size() - 1;
//i对应最高位是第i+1位,res就增加2^(i+1)个数:(1<<i)+res.get(j)
//j为倒序循环(size+1)次,会镜像提取上一次循环产生的结果
//01 - 32: 镜像+2 0132 - 6754: 镜像+4
//0 1 3 2 6 7 5 4 - 12 13 15 14 10 11 9 8: 镜像+8
for (int j = size; j >= 0; j--) {
res.add((1<<i) + res.get(j));
}
}
return res;
}
}
Recursion
public class Solution {
public List<Integer> grayCode(int n) {
List<Integer> res = new ArrayList<Integer>();
if (n == 0) {
res.add(0);
return res;
}
res = grayCode(n-1);
for (int i = res.size()-1; i >= 0; i--) {
int num = res.get(i);
res.add(num+(1<<(n-1)));
}
return res;
}
}
Using XOR
public class Solution {
public List<Integer> grayCode(int n) {
List<Integer> res = new ArrayList<Integer>();
for(int i = 0; i < Math.pow(2,n); i++) res.add(i >> 1 ^ i);
return res;
}
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。