```
class Solution(object):
def grayCode(self, n):
if n == 0: return [0]
r = [0, 1]
for i in range(1, n):
l = len(r)
for j in range(l-1, -1, -1):
r.append((1<<i) + r[j])
return r
```