原题地址

二进制码 -> 格雷码:从最右边起,依次与左边相邻位异或,最左边一位不变。例如:

二进制: 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的更多相关文章

  1. [LeetCode] 89. Gray Code 格雷码

    The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...

  2. leetCode 89.Gray Code (格雷码) 解题思路和方法

    The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...

  3. 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 ...

  4. 【一天一道LeetCode】#89. Gray Code

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 The gra ...

  5. 【LeetCode】89. Gray Code 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  6. 【LeetCode】89. Gray Code

    题目: The gray code is a binary numeral system where two successive values differ in only one bit. Giv ...

  7. LeetCode OJ 89. Gray Code

    题目 The gray code is a binary numeral system where two successive values differ in only one bit. Give ...

  8. 【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 ...

  9. 89. Gray Code

    题目: The gray code is a binary numeral system where two successive values differ in only one bit. Giv ...

随机推荐

  1. dispatchTouchEvent & onInterceptTouchEvent & onTouchEvent

    http://www.cnblogs.com/jqyp/archive/2012/04/25/2469758.html dispatchTouchEvent       分发 onInterceptT ...

  2. MongoDb Replica Set中使用的地址

    Unable to connect to a member of the replica set matching the read preference Primary 今天尝试使用MongoDB ...

  3. C# MongoDB--时区问题(差了8小时)

    原因:MongoDB中存储的时间是标准时间UTC +0:00C#的驱动支持一个特性,将实体的时间属性上添加上这个特性并指时区就可以了.例如:[BsonDateTimeOptions(Kind = Da ...

  4. mac里边配置android开发环境,intellij开发工具:

    1 在android的官网下载 android sdk的mac版 http://developer.android.com/sdk/index.html  选择mac的版本 下载后打开sdk-mana ...

  5. js 将json字符串转换为json兑现

    在数据传输过程中,json是以文本,即字符串的形式传递的,而JS操作的是JSON对象,所以,JSON对象和JSON字符串之间的相互转换是关键.例如:JSON字符串:var str1 = '{ &quo ...

  6. for xml path('') 引发的数据不完整

    When you read Extensible Markup Language (XML) data from Microsoft SQL Server by using the SqlDataRe ...

  7. 决策树的基本ID3算法

    一  ID3算法的大致思想 基本的ID3算法是通过自顶向下构造决策树来进行学习的.我们首先思考的是树的构造从哪里开始,这就涉及到选择属性进行树的构造了,那么怎样选择属性呢?为了解决这个问题,我们使用统 ...

  8. oracle 分析函数(笔记)

    分析函数是oracle数据库在9i版本中引入并在以后版本中不断增强的新函数种类.分析函数提供好了跨行.多层次聚合引用值的能力.分析函数所展现的效果使用传统的SQL语句也能实现,但是实现方式比较复杂,效 ...

  9. Android动画解析--XML

    动画类型 Android的animation由四种类型组成 XML中 alpha 渐变透明度动画效果 scale 渐变尺寸伸缩动画效果 translate 画面转换位置移动动画效果 rotate 画面 ...

  10. centos php-fpm nginx配置

    移除旧的软件包:yum remove httpd* php* 安装:yum install php php-fpm yum install php-gd php-mysql php-mbstring ...