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.

For example, given n = 2, return [0,1,3,2]. Its gray code sequence is:

00 - 0
01 - 1
11 - 3
10 - 2

Note:
For a given n, a gray code sequence is not uniquely defined.

For example, [0,2,3,1] is also a valid gray code sequence according to the above definition.

For now, the judge is able to judge based on one instance of gray code sequence. Sorry about that.

思路:

这题其实挺简单,思路可以联想到组合问题中的回溯,改变第n位,然后再利用其余n-1位的组合重新组成n位的组合。如果在改变第n位的时候,保持其余n-1位不动,再保持第n位将其余n-1位的序列反着推回去就OK了。

感觉程序没有写出来的必要,举个栗子反而更清楚 n = 4

0 0 0 0
0 0 0 1
0 0 1 |1
0 0 1 |0

0 1 |1 0
0 1 |1 1
0 1 |0 1
0 1 |0 0

1 |1 0 0
1 |1 0 1
1 |1 1 1
1 |1 1 0

1 |0 1 0
1 |0 1 1
1 |0 0 1
1 |0 0 0

【题解】【排列组合】【回溯】【Leetcode】Gray Code的更多相关文章

  1. [LeetCode] Gray Code 格雷码

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

  2. [leetcode]Gray Code @ Python

    原题地址:https://oj.leetcode.com/problems/gray-code/ 题意: The gray code is a binary numeral system where ...

  3. LeetCode——Gray Code

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

  4. LeetCode:Gray Code(格雷码)

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

  5. LeetCode: Gray Code [089]

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

  6. LeetCode: Gray Code 解题报告

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

  7. [转载]LeetCode: Gray Code

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

  8. [bzoj2729][HNOI2012]排队 题解 (排列组合 高精)

    Description 某中学有 n 名男同学,m 名女同学和两名老师要排队参加体检.他们排成一条直线,并且任意两名女同学不能相邻,两名老师也不能相邻,那么一共有多少种排法呢?(注意:任意两个人都是不 ...

  9. Leetcode Gray Code

    题目的意思就是将十进制转换成格雷码 首先将二进制转换成格雷码 根据此图可以看出二进制的第i和第i+1位异或为格雷码的第i+1位,对于给定的十进制数x,其(x>>1)相当于二进制向右移动一位 ...

随机推荐

  1. 介绍开源的.net通信框架NetworkComms

    Networkcomms 是一款C# 语言编写的TCP/UDP通信框架  作者是英国人  以前是收费的 目前作者已经开源  开源地址是:https://github.com/MarcFletcher/ ...

  2. 基于Storm的工程中使用log4j

    最近使用Storm开发,发现log4j死活打不出debug级别的日志,网上搜到的关于log4j配置的方法都试过了,均无效. 最终发现问题是这样的:最新的storm使用的日志系统已经从log4j切换到了 ...

  3. mysql创建PATH快捷

    1.使其临时生效 PATH=$PATH:/usr/local/mysql/bin 2.永久生效 编辑/etc/profile  添加第79列 然后source /etc/profile 3.输入命令m ...

  4. 第三章 XHTML 表单

    1.表单的主要作用在于在网页上提供一个图形用户界面,以采集和提交用户输入的数据. 2.HTML表单元素和属性可以分为两种类型:定义表单整体结构,使浏览器知道如何处理表单数据的元素:创建输入控件的元素. ...

  5. 绑定本地Service并与之通信-----之一

    import android.app.Service;import android.content.Intent;import android.os.Binder;import android.os. ...

  6. LinearLayout练习

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...

  7. C#Winform从页面获取数据,传入数据库

    环境: 1.SQLite数据库新建数据表,设置相应的字段.(其他数据库形式都相似,using相应数据库的包即可) 2.页面有两个textBox:textBox1.textBox2, 3.一个保存按钮: ...

  8. Linux gcc编译(动态库,静态库)

    1. linux 库路径: /lib , /usr/lib , /usr/local/lib 2.linux 编译静态库 a.编写源文件vi pr1.c void print1(){    print ...

  9. C++-标准输入输出

    1,cout 1) 用来向标准输出打印. 2) 如果参数是char*类型,则直接输出字符串.如果想要输出地址,则需要强制转换: <<static_cast<void*>(con ...

  10. 破解Xamarin

    试用了一阵子Mono For Android,今天到期了,,囊中羞涩,只好破解. 说是要在vs2013的英文界面下运行破解包,不知道是真是假,下载并安装了一个. 然后又下载了破解包.是个名为xa.ra ...