Leetcode#89 Gray Code
二进制码 -> 格雷码:从最右边起,依次与左边相邻位异或,最左边一位不变。例如:
二进制: 1 0 0 1 1 1 0
|\|\|\|\|\|\|
格雷码: 1 1 0 1 0 0 1
从二进制到格雷码有一个残暴公式: a ^ (a << 1)
格雷码 -> 二进制码:从左边第二位开始,依次与左边相邻已经解码位异或,最左边一位不变。例如:
格雷码: 1 1 0 1 0 0 1
\| | | | | |
1 | | | | |
1 0\| | | | |
1 0 | | | |
1 0 0\| | | |
1 0 0 | | |
1 0 0 1\| | |
1 0 0 1 | |
1 0 0 1 1\| |
1 0 0 1 1 |
1 0 0 1 1 1\|
1 0 0 1 1 1
二进制: 1 0 0 1 1 1 0
可惜从格雷码到二进制没有残暴公式
如何从一个格雷码生成下一个格雷码呢?规则如下:
1. 从0开始
2. 翻转最右位(0变1,1变0)
3. 翻转最右边是1的左边那一位,比如10010 -> 10010
4. 回到2
比如要生成4bit的格雷码,按照上面的规则:
步骤 格雷码 编号
1 0 0 0 0 (1)
|
2 0 0 0 1 (2)
|
3 0 0 1 (3)
|
2 0 0 1 0 (4)
|
3 0 1 0 (5)
|
2 0 1 1 1 (6)
|
3 0 1 1 (7)
|
2 0 1 0 0 (8)
|
3 1 0 0 (9)
|
2 1 1 0 1 (10)
|
3 1 1 1 (11)
|
2 1 1 1 0 (12)
|
3 1 1 0 (13)
|
2 1 0 1 1 (14)
|
3 1 0 1 (15)
|
2 1 0 0 0 (16)
代码:
vector<int> grayCode(int n) {
vector<int> res;
int ub = ( << n) - ;
int c = ; for (int i = ; i <= ub; i++) {
c = (i & ) ? c ^ : c ^ ((c & -c) << );
res.push_back(c);
} return res;
}
Leetcode#89 Gray Code的更多相关文章
- [LeetCode] 89. Gray Code 格雷码
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- leetCode 89.Gray Code (格雷码) 解题思路和方法
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- 89. Gray Code - LeetCode
Question 89. Gray Code Solution 思路: n = 0 0 n = 1 0 1 n = 2 00 01 10 11 n = 3 000 001 010 011 100 10 ...
- 【一天一道LeetCode】#89. Gray Code
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 The gra ...
- 【LeetCode】89. Gray Code 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】89. Gray Code
题目: The gray code is a binary numeral system where two successive values differ in only one bit. Giv ...
- LeetCode OJ 89. Gray Code
题目 The gray code is a binary numeral system where two successive values differ in only one bit. Give ...
- 【LeetCode】89. Gray Code (2 solutions)
Gray Code The gray code is a binary numeral system where two successive values differ in only one bi ...
- 89. Gray Code
题目: The gray code is a binary numeral system where two successive values differ in only one bit. Giv ...
随机推荐
- angularJS 数组更新时重新排序之解决方案一:这个坑,绕开吧,不跳了……
今天产品大人发现了一bug,图表数据和数据库总是对不上,原因是当前端更新数组时,angularJS默认对数组进行了排序. // 点击事件:input复选框 $scope.fnClickUpdateAr ...
- 纯js拖拽参考
function myDrag(obj){ obj.onmousedown=function(e){ var e=e||window.event; var diffX=e.clientX-this.o ...
- JDK 环境变量的配置
小编下载的是 jdk1.6 的版本 具体如何如何安装我相信大家肯定都会的了 这里就不做详细的说明了 , 我的jdk 安装的目录是在 D:\javac 这个目录下 下面我们来配置环境变量 在我的电脑- ...
- 透过c的编程原则,来规范自己现在的一些编程习惯
1.合理的使用注释 注释为:/*…………*/ 注释有以下几种情况: 1) 版本.版权声明. 2) 函数接口说明. 3) 重要的代码或者段落显示. 注释注意: 1) 注释是对代码的解释,不是对文档.注释 ...
- cursorfilter
android.widget.CursorAdapter它首先实现了两个接口Filterable,CursorFilter.CursorFilterClient.其中Filterable接口定义了ge ...
- 网页绘制图表 Google Charts with JavaScript #2 ....与ASP.NET网页结合 (ClientScriptManager.RegisterStartupScript 方法)
此为文章备份,原文出处(我的网站) 网页绘制图表 Google Charts with JavaScript #2 ....与ASP.NET网页结合 (ClientScriptManager.Regi ...
- a 标签 跳转4种类型
<a href='' target=''>中的target有4种参数: '_self' , '_parent' , '_top' 和 '_blank' 在没有使用框架 ...
- opensuse 安装 Anaconda3 之后出现Could not start d-bus. Can you call qdbus?
最近在安装了opensue Leap42.1之后,想要学习一下python,就安装了Anaconda3,并且将Anaconda3的安装路径添加到了PATH里,但是在重新启动系统后,出现了"C ...
- Python学习教程(learning Python)--1.2.3 Python格式化输出百分比
在有些情况下,需要百分比输出数据,我们可以继续使用Python内建函数format来实现百分比的数据输出. >>> print(format(0.5236, '.2%')) 其结果如 ...
- autolayout 总结
hasAmbiguousLayoutexerciseAmbiguityInLayout_autolayoutTracerecursiveDescription 第一步:更新约束,可以被认为是一个“计量 ...