public class Solution {
public List<Integer> grayCode(int n) {
// Reflect-and-prefix
// nth: n xor n/2
List<Integer> gray = new ArrayList<Integer>();
if(n == 0) {
gray.add(0);
return gray;
}
gray = grayCode(n - 1);
int size = gray.size();
for(int i = size - 1; i >= 0; i--) {
gray.add((1 << (n - 1)) | gray.get(i));
}
return gray;
}
}
Time: O(n^2)
Space: O(n)