class Solution(object):
def grayCode(self, n):
"""
:type n: int
:rtype: List[int]
"""
if n==0:
return [0]
ret=[0,1]
for i in xrange(1,n):
prev=2**i
lastind = len(ret)-1
for v in xrange(lastind+1):
ret.append(ret[lastind-v]+prev)
return ret

本文介绍了一种生成灰码序列的算法实现,灰码是一种二进制数字系统中相邻两个数值仅有一位不同的编码方式。通过递归算法,该文详细解释了如何生成任意位数的灰码序列。

被折叠的 条评论
为什么被折叠?



