【题解】【排列组合】【回溯】【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)相当于二进制向右移动一位 ...
随机推荐
- 推荐10款非常优秀的 HTML5 开发工具
HTML5 发展如火如荼,随着各大浏览器对 HTML5 技术支持的不断完善以及 HTML5 技术的不断成熟,未来 HTML5 必将改变我们创建 Web 应用程序的方式.今天这篇文章向大家推荐10款 ...
- 指定socket文件连接mysql
1.利用ps aux |grep mysql 查看--socket 路径 2.创建软连接.创建文件 3.登录成功
- ZOJ 3646 Matrix Transformer 二分匹配,思路,经典 难度:2
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4836 因为要使对角线所有元素都是U,所以需要保证每行都有一个不同的列上有U,设 ...
- mac 安装memcached服务
使用homebrew安装,homebrew安装方法http://brew.sh/ 安装memcached服务 brew install memcached 配置开机启动(用brew安装之后下面会提示怎 ...
- K2 BPM医疗行业EMS解决方案
EMS,即Event Management System,K2医疗行业EMS解决方案包括四方面的内容. 详情链接:http://www.k2software.cn/zh-hans/ems-soluti ...
- Problem C HDU 5224
Description There is a piece of paper in front of Tom, its length and width are integer. Tom knows t ...
- [微软]technet与msdn
我们搜索一个微软术语,有时定位到technet页面,有时定位到msdn页面.我直观的理解就是technet教人们如何使用微软产品,而msdn指导人们如何开发基于微软产品的软件.那么微软对它们具体定位是 ...
- SharePoint 2010 BCS - 简单实例(二)外部列表创建
博客地址 http://blog.csdn.net/foxdave 接上篇 由于图片稍多篇幅过长影响阅读,所以分段来写. 添加完数据源之后,我们需要为我们要放到SharePoint上的数据表定义操作, ...
- JSP重定向传递参数
我一个JSP程序,要实现前台提交数据给后台处理后,后台jsp自动跳转到另一个jsp页面,这种方式也叫重定向,重定向的方法有多种,暂时我试过的并且能成功的有两个: 一种是用 response.sendR ...
- 2016 - 1 -17 GCD主队列与全局队列
一:主队列 1.概念:每一个应用程序对应唯一一个主队列,直接GET即可:在多线程开发中,使用主队列更新UI dispatch_queue_t q = dispatch_get_main_queue() ...