描述:

要求相邻数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. 功能丰富的 Perl:轻松调试 Perl

    http://www.ibm.com/developerworks/cn/linux/sdk/perl/culture-4/index.html

  2. Java基础知识强化之多线程笔记03:进程与线程 和 多线程的意义

    1. 要想了解多线程,必须先了解线程,而要想了解线程,必须先了解进程,因为线程是依赖于进程而存在. 2. 什么是进程? 通过任务管理器我们就看到了进程的存在. 而通过观察,我们发现只有运行的程序才会出 ...

  3. 对JavaScript对象数组按指定属性和排序方向进行排序

    引子 在以数据为中心的信息系统中,以表格形式展示数据是在常见不过的方式了.对数据进行排序是必不可少的功能.排序可以分为按单个字段排序和按多个字段不同排序方向排序.单字段排序局限性较大,不能满足用户对数 ...

  4. MySQL创建/删除/清空表,添加/删除字段

    创建表: create table tablename (column_name column_type); create table table_name( id int not null auto ...

  5. js解析XML

    //在当前页面内追加换行标签和指定的HTML内容function w( html ){    $(document.body).append("<br/>" + htm ...

  6. D3中path各指令的含义

    svg.append('path').attr({ id: 'mypath', d: 'M50 100Q350 50 350 250Q250 50 50 250' }) path 的指令有: 指令 参 ...

  7. PV模型

    你想建设一个能承受500万PV/每天的网站吗? 500万PV是什么概念?服务器每秒要处理多少个请求才能应对?如果计算呢? 一.PV是什么 PV是page view的简写.PV是指页面的访问次数,每打开 ...

  8. 创建eclipse新的workspace并设置workspace共享配置

    一:创建新的workspace 1.File——Switch Workspace——Other 2.修改workspace路径和名称 3.修改后如下: 4.点击OK按钮后,eclipse自动重启 同时 ...

  9. JavaGUI实现点名系统

    有一个名字数组,根据线程知识是框中的数据依次修改 package Week1008; import java.awt.Font; import java.awt.event.ActionEvent; ...

  10. jsp的useBean标签使用

    创建JavaBean package com.itheima.domain; public class Person { private String name; private int age; p ...