```
class Solution:
# @param {integer} n
# @return {integer[]}
def grayCode(self, n):
# set the result as [0] when n = 0
result = [0]
# based on obervation
# n = 0, [0]
# n = 1, [0, 1]
# n = 2, [0, 1, 3, 2]
# n = 3, [0, 1, 3, 2, 6, 4, 7, 5]
# each time just append the original list with the revesed list and add 2 ** (n-1)
for i in range(n):
for j in range(2 ** i):
result.append(result[2 ** i - j - 1] + 2 ** i)
return result
```