【题解】【排列组合】【回溯】【Leetcode】Gray Code
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的更多相关文章
- [LeetCode] Gray Code 格雷码
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- [leetcode]Gray Code @ Python
原题地址:https://oj.leetcode.com/problems/gray-code/ 题意: The gray code is a binary numeral system where ...
- LeetCode——Gray Code
Description: The gray code is a binary numeral system where two successive values differ in only one ...
- LeetCode:Gray Code(格雷码)
题目链接 The gray code is a binary numeral system where two successive values differ in only one bit. Gi ...
- LeetCode: Gray Code [089]
[题目] The gray code is a binary numeral system where two successive values differ in only one bit. Gi ...
- LeetCode: Gray Code 解题报告
Gray CodeThe gray code is a binary numeral system where two successive values differ in only one bit ...
- [转载]LeetCode: Gray Code
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- [bzoj2729][HNOI2012]排队 题解 (排列组合 高精)
Description 某中学有 n 名男同学,m 名女同学和两名老师要排队参加体检.他们排成一条直线,并且任意两名女同学不能相邻,两名老师也不能相邻,那么一共有多少种排法呢?(注意:任意两个人都是不 ...
- Leetcode Gray Code
题目的意思就是将十进制转换成格雷码 首先将二进制转换成格雷码 根据此图可以看出二进制的第i和第i+1位异或为格雷码的第i+1位,对于给定的十进制数x,其(x>>1)相当于二进制向右移动一位 ...
随机推荐
- ruby在线学习
http://tryruby.org/ [Heroku空间] 免费ruby空间
- PHP 长字符串替换操作性能(替换多换注释的代码)
因为写一个css合并工具,去掉注释时,发现正则表达式很难写,最后,还是用php来实现吧,但是一不小心写出来的代码居然报内存超出可用的128m!! 原因是因为我找到/*和*/之后 $str=substr ...
- 二模 (12)day2
第一题: 题目大意: 有N颗糖,两个人轮流取,每次只能取质数颗,不能取的输.求先取者若必胜,最少需要多少步胜利.(N<=10000) 解题过程: 1.看到N的范围比较小,先打个素数表,然后dp即 ...
- WEKA使用教程(界面工具的用法)
WEKA使用教程 目录 1. 简介2. 数据格式3.数据准备4. 关联规则(购物篮分析)5. 分类与回归6. 聚类分析 1. 简介 WEKA的全名是怀卡托智能分析环境(Waikato Environm ...
- BeanUtils组件
引入jar包(需要引入依赖的日志jar包) Person p = new Person(); p.setName("Daisy"); p.setAge(12); //对象的copy ...
- .net 小技巧
简单提示效果: <input runat="server" type="text" id="SelPerson" value=&quo ...
- MJPhotoBrowser 两个bug:回到小图模式时会闪动&大图太靠近底部
最近项目需要写网络的相片视频浏览的库, 没时间重写,使用了MJPhotoBrowser,里面的一些bug 和解决写在下面 1.-[MJPhotoLoadingView setProgress:]: m ...
- 【django入门教程】Django的安装和入门
很多初学django的朋友,都不知道如何安装django开发以及django的入门,今天小编就给大家讲讲django入门教程. 注明:python版本为3.3.1.Django版本为1.5.1,操作系 ...
- 一篇介绍jquery中的ajax的结合
<script type="text/javascript"> function Text_ajax() { $.aja ...
- Java基础毕向东day02
1. 常量 null 等特殊 2.标识符 数字-字母-下划线,数字不能开头 3.二进制 1> 二进制计算方法. 2>常用二进制. 1 1 0 0 1 ...