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 ...
随机推荐
- Oracle删除表主键语句
删除无名主键语句: SELECT * FROM USER_CONS_COLUMNS WHERE TABLE_NAME = '表名'; ALTER TABLE 表名 DROP CONSTRAINT 主键 ...
- linux 常用命令及技巧
linux 常用命令及技巧 linux 常用命令及技巧:linux 常用命令总结: 一. 通用命令: 1. date :print or set the system date and time 2. ...
- Php output buffering缓存及程序缓存
在php中有时为了控制程序的输出显示顺序,提供了output buffering缓存(php自身缓存机制). 若Ob缓存开启,需要输出的就先存在ob缓存里,再到程序缓存里.若没有开启,则直接进入 ...
- Android VideoView简单播放视频
给Android VideoView一个文件目录,就可以直接播放智能设备中的视频文件,现在以播放事先用手机拍好并重命名的视频文件test.mp4为例.(1) 需要在布局文件中写一个ViedoView: ...
- Ubuntu安装wps for linux
1.WPS For Linux 2013 还是只提供了32位版本,我用的是 64位 Ubuntu,如果您也是64位系统,还需要提前安装一些32位的库文件. sudo apt-get install i ...
- Java4Android
变量 在计算机中存储信息需要声明变量的位置和所需的内存空间 boolean true false char ASCII字符集 计算机中所有数据都使用二进制表示 例如:a,b,c 适用七位二进制数进行表 ...
- Java 第四天 Mysql
下载地址 http://dev.mysql.com/downloads/ 社区版是免费的 配置将zip 解压copy到本地,如:C:\mysql-5.6.15-winx64,复制配置文件my-def ...
- UCOS2_STM32F1移植详细过程(一)
Ⅰ.概述 该文写针对初学µC/OS的朋友,基于以下平台来一步一步移植µC/OS嵌入式操作系统.UCOS移植相关平台: 系统平台:µC/OS-II (最新V2.92版) 硬件平台:STM32F1 ...
- rails笔记
rake -T 列出全部taskconfig.active_record.schema_format = :sql #remove the old db/schema.rb file, create ...
- LaTeX中无法显示中文问题