leetcode-89-格雷编码*

题目描述:

方法一:

class Solution:
    def grayCode(self, n: int) -> List[int]:
        gray = [0]
        for i in range(n):
            add = 2**i
            for j in range(len(gray)-1,-1,-1):
                gray.append(add+gray[j])
        return gray

方法二:

class Solution: 
    def grayCode(self, n: int) -> List[int]: 
        res = [] 
        for i in range(2 ** n): 
            res.append((i >> 1) ^ i) 
        return res

 

posted @ 2019-07-13 13:59  oldby  阅读(135)  评论(0编辑  收藏  举报