题目描述:

leetcode-86-格雷编码

leetcode-86-格雷编码

方法一:

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

 

相关文章:

  • 2021-08-12
  • 2022-02-27
  • 2021-08-11
  • 2022-12-23
  • 2022-01-11
  • 2021-04-23
猜你喜欢
  • 2022-02-02
  • 2021-11-18
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-23
  • 2021-05-06
相关资源
相似解决方案