描述:

要求相邻数2进制差一位

先获得n-1的列表表示小于 2^(n-1) 的符合要求的列表,加上最高位的加成 2^(n-1) 就是大于等于 2^(n-1) 的符合要求的列表,后者翻转一下就能够与前者连接上了

代码:

 class Solution:
# @return a list of integers
def grayCode(self, n):
if n == 0: return [0] s1 = self.grayCode(n - 1)
s2 = [item + 2 ** (n - 1) for item in s1[::-1]]
s1.extend(s2) return s1

#Leet Code# Gray Code的更多相关文章

  1. 89. Gray Code(中等,了解啥是 gray code)

    知道啥是 gray code 就是收获了. 下面介绍了 gray code 发明的 motivation, 了解动机后就知道啥是 gray code 了. https://zh.wikipedia.o ...

  2. [LeetCode] Gray Code 格雷码

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

  3. 【LeetCode】Gray Code

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

  4. Gray Code

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

  5. 【leetcode】Gray Code (middle)

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

  6. [LintCode] Gray Code 格雷码

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

  7. 44. Decode Ways && Gray Code

    Decode Ways A message containing letters from A-Z is being encoded to numbers using the following ma ...

  8. LeetCode——Gray Code

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

  9. LeetCode:Gray Code(格雷码)

    题目链接 The gray code is a binary numeral system where two successive values differ in only one bit. Gi ...

随机推荐

  1. android127 zhihuibeijing 屏幕适配

    ## 屏幕适配 ## 加载不同分辨率的图片是根据手机的像素来加载不同分辨率文件夹下的图片. > 先在主流屏幕来发: *(分辨率和手机屏幕大小无关), 遵循原则: 不用AbsoluteLayout ...

  2. MySQL架构优化实战系列2:主从复制同步与查询性能调优

  3. MS OFFICE 2010破解版安装

    受人所托发布一个MS OFFICE 2010破解版的傻瓜安装教程,刚好新本本也没有安装,安装过程中截了几个图就搞定了. 安装包: http://www.itopdog.cn/soft/office20 ...

  4. Matlab plotyy函数的使用及问题总结

    MATLAB函数,用来绘制双纵坐标图. 调用格式: 1.plotyy(X1,Y1,X2,Y2):以左.右不同纵轴绘制X1-Y1.X2-Y2两条曲线. 2.plotyy(X1,Y1,X2,Y2,FUN1 ...

  5. MATLAB基础入门笔记

    为了参加那个电工杯,豁出去啦,时间真的很短,但是得挑战呀..对于MATLAB编程,有一些了解,MATLAB(矩阵实验室的简称)是一种专业的计算机程序,用于工程科学的矩阵数学运算,说说它的开发环境. 任 ...

  6. JavaScript类型检测, typeof操作符与constructor属性的异同

    *#type.js function Person(name, age) { this.name = name; this.age = age; } var d = {an: 'object'}; v ...

  7. h2database源码浅析:事务、两阶段提交

    Transaction Isolation Transaction isolation is provided for all data manipulation language (DML) sta ...

  8. Submine Text 3 代码自动换行

    开启自动换行功能的步骤: 1.打开 Preferences -> Setting - User(设置 - 用户) 2.在再大的括号前添加如下内容即可: "word_wrap" ...

  9. mysql关键字讲解(join 、order by、group by、having、distinct)

    1.join     1.1 OUTER JOIN:想要包含右侧表中的所有行,以及左侧表中有匹配记录的行.        1.11 Mysql中有左连接(left join):            ...

  10. asp.net Hierarchical Data

    Introduction A Hierarchical Data is a data that is organized in a tree-like structure and structure ...