描述:

要求相邻数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. img图片下有个间隙是为什么

    转自知乎:http://www.zhihu.com/question/21558138要理解这个问题,首先要弄明白CSS对于 display: inline 元素的 vertical-align 各个 ...

  2. 文件I/O之sync、fsync和fdatasync函数

    传统的UNIX实现在内核中设有缓冲区高速缓存或页面高速缓存,大多数磁盘I/O都通过缓冲进行.当将数据写入文件时,内核通常先将数据复制到其中一个缓冲区中,如果 该缓冲区尚未写满,则并不将其排入输出队列, ...

  3. linux之ubuntu下php环境配置

    本文主要说明如何在Ubuntu下配置PHP开发环境LAMP.   Ubuntu 搭建 php 环境   所谓LAMP:Linux,Apache,Mysql,PHP   安装 Apache2:(注意可以 ...

  4. java_jdbc_基本连接池

    MyDataSource实现封装连接池,MyConnection实现connection类,通过代理模式相互调用 package cn.itcast; import java.sql.*; publi ...

  5. C# 之 服务端获取远程资源

    获取指定网页的远程资源可使用 WebClient.WebRequest.HttpWebRequest 三种方式来实现.当然也可使用webBrowse,webBrowse不做介绍. 通过 System. ...

  6. Disruptor框架

    http://developer.51cto.com/art/201306/399370.htm

  7. DHCP 服务器

    简介: DHCP(Dynamic Host Configuration Protocol,动态主机配置协议)是一个局域网的网络协议,使用UDP协议工作,主要用途:给内部网络或网络服务供应商自动分配 I ...

  8. 关于Struts2中的值栈与OGNL表达式

    1.1.1    OGNL概述: Object Graphic Navigation Language(对象图导航语言)的缩写 * EL     :OGNL比EL功能强大很多倍. 它是一个开源项目. ...

  9. JMS简介

    任何一个系统从整体上来看,其实质就是由无数个小的服务或事件(我们可以称之为事务单元)有机地组合起来的.对于系统中任何一个比较复杂的功能,都是通过调用各个独立的事务单元以实现统一的协调运作而实现的.现在 ...

  10. 将Word、Excel内容显示在Winform界面

    这里使用到dsoframer.ocx插件 1. 首先下载dsoframer.ocx插件,下载地址: http://pan.baidu.com/s/1kTKHeIj 2. 注册该插件 Win7的做法是按 ...