链接:
https://leetcode-cn.com/problems/gray-code/comments/
这道题只要掌握格雷编码的规律就很好写
class Solution {
public List<Integer> grayCode(int n) {
List<Integer> result = new ArrayList<Integer>();
result.add(0);
for(int i = 1;i<=n;i++)
{
for(int j = (int)Math.pow(2,i-1);j<(int)Math.pow(2,i);j++)
{
result.add(result.get((int)Math.pow(2,i)-1-j)+(int)Math.pow(2,i-1));
}
}
return result;
}
}