2 solutions
class Solution:
def grayCode(self, n):
"""
:type n: int
:rtype: List[int]
"""
return map(lambda x:int(x,2),self.gray(n))
def gray(self,n):
res=None
if(n==0):
res=['0']
elif(n==1):
res=['0','1']
else:
tmp=self.gray(n-1)
res=['0'+ x for x in tmp]+['1'+x for x in tmp[::-1]]
return res
class Solution:
# @return a list of integers
'''
from up to down, then left to right
0 1 11 110
10 111
101
100
start: [0]
i = 0: [0, 1]
i = 1: [0, 1, 3, 2]
i = 2: [0, 1, 3, 2, 6, 7, 5, 4]
'''
def grayCode(self, n):
results = [0]
for i in range(n):
results += [x + pow(2, i) for x in reversed(results)]
return results