Solution URL
https://leetcode.com/submissions/detail/413893729/
代码
class Solution {
public List<Integer> grayCode(int n) {
//ans:https://leetcode-cn.com/problems/gray-code/solution/gray-code-jing-xiang-fan-she-fa-by-jyd/
List<Integer> gray = new ArrayList<>();
gray.add(0);
for(int i = 0; i < n; i++){
int head = 1 << i;
for(int j = gray.size() - 1; j >= 0; j--){
//上半部分一样,不用动,只要添加下半部分就可以
//下半部分就是上半部分逆序,前面加1
gray.add(head + gray.get(j));
}
}
return gray;
}
}