力扣—gray code (格雷编码) python实现

题目描述:编码

中文:spa

格雷编码是一个二进制数字系统,在该系统中,两个连续的数值仅有一个位数的差别。code

给定一个表明编码总位数的非负整数 n,打印其格雷编码序列。格雷编码序列必须以 0 开头。blog

英文:it

The gray code is a binary numeral system where two successive values differ in only one bit.io

Given a non-negative integer n representing the total number of bits in the code, print the sequence of gray code. A gray code sequence must begin with 0.class

class Solution(object):
    def grayCode(self, n):
        """
        :type n: int
        :rtype: List[int]
        """
        res = [0]
        for i in range(n):
            res += [ (1 << i) + res[-j-1] for j in range(1 << i) ]
        return res

 

 

题目来源:力扣object

相关文章
相关标签/搜索