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 ...
随机推荐
- spark-submit工具参数说明
执行时需要传入的参数说明 Usage: spark-submit [options] <app jar | python file> [app options] 参数名称 含义 --mas ...
- php敏感词过滤
在项目开发中发现有个同事在做敏感词过滤的时候用循环在判断,其实是不用这样做的,用php的数组函数和字符串函数即可实现 function filterNGWords($string) { $badwor ...
- 安装Spark集群(在CentOS上)
环境:CentOS 6.4, Hadoop 1.1.2, JDK 1.7, Spark 0.7.2, Scala 2.9.3 1. 安装 JDK 1.7 yum search openjdk-deve ...
- 生日蛋糕 (codevs 1710) 题解
[问题描述] 7月17日是Mr.W的生日,ACM-THU为此要制作一个体积为Nπ的M层生日蛋糕,每层都是一个圆柱体. 设从下往上数第i(1<=i<=M)层蛋糕是半径为Ri,高度为Hi的圆柱 ...
- 蓝牙4.0LED灯控方案
一.LED照明机遇 相对传统光源产品,LED灯凭借其光效高.寿命长.不含汞.总拥有成本低等优势,已被普遍认为是一种革命性和替代性的技术.随着全球白炽灯禁产.禁用政策的依次落实,白炽灯将逐渐消失于市场. ...
- linux内核SPI总线驱动分析(二)(转)
简而言之,SPI驱动的编写分为: 1.spi_device就构建并注册 在板文件中添加spi_board_info,并在板文件的init函数中调用spi_register_board_info(s3 ...
- 第三章 管理程序流(In .net4.5) 之 实现程序流
1. 概述 本章内容包括 布尔表达式.流控制方式.集合遍历 以及 流跳转. 2. 主要内容 *由于该章内容比较基础,日常用的也很多,故对一些常用的基础内容不再赘述. 2.1 使用布尔表达式 熟悉下列比 ...
- AngularJs记录学习02
<!doctype html> <html ng-app="myapp"> <head> <meta http-equiv="C ...
- flask程序部署在openshift上的一些注意事项
https://www.openshift.com/blogs/how-to-install-and-configure-a-python-flask-dev-environment-deploy-t ...
- Reverse Vowels of a String
Write a function that takes a string as input and reverse only the vowels of a string. Example 1:Giv ...