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 ...
随机推荐
- jQuery类级别插件--返回顶部,底部,去到任何部位
先引入js:<script type="text/javascript" src="jquery.js" ></script><s ...
- canvas 绘制矩形和圆形
canvas绘制有两神方法:1).填充(fill)填充是将图形内部填满. 2).绘制边框 (stroke)绘制边框是不把图形内部填满,只是绘制图形的外框. 当我们在绘制图形的时候,首先要设定好绘制的样 ...
- Linux服务器的初步配置流程
作者: 阮一峰 日期: 2014年3月14日 开发网站的时候,常常需要自己配置Linux服务器. 本文记录配置Linux服务器的初步流程,也就是系统安装完成后,下一步要做的事情.这主要是我自己的总结和 ...
- php验证手机号码
大家都应该有这个常识,中国的手机号码都是以数字“1”开头,然后利用“0~9”10个数字组成的11位数字组合,那么我们的验证规则肯定要根据这个思路来写. 根据上面的简单思路,我们便可以写下以下的验证代码 ...
- SRF之数据验证
实现表单输入数据的验证,包括客户端验证和服务器端验证 如何使用 数据验证在业务层的实体类字段上增加数据验证的特性,例如 public class User { [Required(ErrorMessa ...
- equals函数
equals函数在Object类当中,而Object类是所有类的父类,所以所有的类里面都有equals函数. “==”操作符之前用于比较两个基本数据类型的值是否相等,而对于引用数据类型,“==”操作符 ...
- Java 打印金字塔 or 打印带数字的金字塔 (Java 学习中的小记录)
Java 打印金字塔 or 打印带数字的金字塔 (Java 学习中的小记录) 作者:王可利(Star·星星) 效果图: 代码如下: class Star8 { public static void m ...
- PAT乙级真题1003. 我要通过!(20)(解题)
“答案正确”是自动判题系统给出的最令人欢喜的回复.本题属于PAT的“答案正确”大派送 —— 只要读入的字符串满足下列条件,系统就输出“答案正确”,否则输出“答案错误”. 得到“答案正确”的条件是: 1 ...
- L2-015. 互评成绩
学生互评作业的简单规则是这样定的:每个人的作业会被k个同学评审,得到k个成绩.系统需要去掉一个最高分和一个最低分,将剩下的分数取平均,就得到这个学生的最后成绩.本题就要求你编写这个互评系统的算分模块. ...
- uva 11538 Chess Queen<计数>
链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&am ...