1.递归的思想:每一个问题可以转化为小一位前面加上0或者1的问题
2.记得加1的时候要反向输出
class Solution(object):
def grayCode(self, n):
"""
:type n: int
:rtype: List[int]
"""
return map(lambda x: int(x, 2), self.bit_gray(n))
def bit_gray(self, n):
ans = None
if n == 0:
ans = ['0']
elif n == 1:
ans = ['0', '1']
else:
pre_gray = self.bit_gray(n - 1)
ans = ['0' + x for x in pre_gray] + ['1' + x for x in pre_gray[::-1]]
return ans
1.int(x,base):第二个参数base=2;说明要int函数把字符串’12345’视为二进制数转换成十进制数
print int(‘12345’,8) # 5349
2.
g = lambda x:x+1
g(1)
>>>2
g(2)
>>>3