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, print the sequence of gray code. A gray code sequence must begin with 0.
For example, given n = 2, return [0,1,3,2]
. Its gray code sequence is:
00 - 0 01 - 1 11 - 3 10 - 2
Note:
For a given n, a gray code sequence is not uniquely defined.
For example, [0,2,3,1]
is also a valid gray code sequence according to the above definition.
For now, the judge is able to judge based on one instance of gray code sequence. Sorry about that.
半天没有读懂题意,不知Gray Code 为何物?English水平差劲之极,xx参考了:http://www.cnblogs.com/TenosDoIt/p/3451938.html
二进制转格雷码:gray = (binary) xor (binary >> 1)
格雷码表示n为的bit位上,第一个序列为0....0,第二个序列就有一位与第一位不同即有一位为一,0....1...0
public class Solution {
public ArrayList<Integer> grayCode(int n) {
ArrayList<Integer> res = new ArrayList<Integer>();
if(n == 0){
res.add(0);
return res;
}
int num = 1<<n;
for(int i=0; i<num; i++){
int t = i ^ (i>>1);
res.add(t);
}
return res;
}
}