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

题目描述:

中文:

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

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

英文:

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

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 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

 

 

题目来源:力扣

 

posted @ 2019-09-27 15:12  hehe哒  阅读(402)  评论(0编辑  收藏  举报