class Solution { public List<Integer> grayCode(int n) { List<Integer> list = new ArrayList<>((int) Math.pow(2, n)); list.add(0); int powVal = 1; for (int i = 0; i < n; i++) { for (int j = list.size() - 1; j >= 0; j--) { list.add(powVal + list.get(j)); } powVal <<= 1; }
return list; } }
|