public static List<Integer> grayCode(int n) {
List<Integer> res = new ArrayList<>();
for (int i = 0; i <(1 << n); i++) {
int temp = (i/2)^i;
res.add(temp);
}
return res;
}
Easy to understand by Java, and o(n) time complexity ! but only defeat 20%! and after I change the Math.pow(2,n) to (1 << n), defeat 51% immediately!




@zhanglifeng get it ! Your solution is really very good. you can write like this
int size = 1 << n;
for (int i = 0; i < size; i++) {} it will be faster.
