LeetCode:Gray Code(格雷码)
The gray code is a binary numeral system where two successive values differ in only one bit.
Given a non-negative integer n representing the total number of bits in the code, print the sequence of gray code. A gray code sequence must begin with 0.
For example, given n = 2, return [0,1,3,2]
. Its gray code sequence is:
00 - 0
01 - 1
11 - 3
10 - 2
Note:
For a given n, a gray code sequence is not uniquely defined.
For example, [0,2,3,1]
is also a valid gray code sequence according to the above definition.
For now, the judge is able to judge based on one instance of gray code sequence. Sorry about that. 本文地址
二进制转格雷码:gray = (binary) xor (binary >> 1)
代码如下:
class Solution {
public:
vector<int> grayCode(int n) {
//注意n = 0时,输出{0}而不是空数组
int num = <<n;
vector<int> res;
res.reserve(num);
for(int i = ; i < num; i++)
res.push_back(i^(i>>));
return res;
}
};
这篇文章有一个对格雷码很有意思的解释
顺便科普一下解码(格雷码 转 二进制码)方法(摘自百度百科):

(G:格雷码,B:二进制码)
举例:
如果采集器器采到了格雷码:1010
就要将它变为自然二进制:
0 与第四位 1 进行异或结果为 1
上面结果1与第三位0异或结果为 1
上面结果1与第二位1异或结果为 0
上面结果0与第一位0异或结果为 0
因此最终结果为:1100 这就是二进制码即十进制 12
当然人看时只需对照表1一下子就知道是12
|
【版权声明】转载请注明出处:http://www.cnblogs.com/TenosDoIt/p/3451938.html
LeetCode:Gray Code(格雷码)的更多相关文章
- [LeetCode] 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 ...
- gray code 格雷码 递归
格雷码 the n-1 bit code, with 0 prepended to each word, followd by the n-1 bit code in reverse order, w ...
- [LintCode] Gray Code 格雷码
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- Gray Code - 格雷码
基本概念 格雷码是一种准权码,具有一种反射特性和循环特性的单步自补码,它的循环.单步特性消除了随机取数时出现重大误差的可能,它的反射.自补特性使得求反非常方便.格雷码属于可靠性编码,是一种错误最小化的 ...
- HDU 5375 Gray code 格雷码(水题)
题意:给一个二进制数(包含3种符号:'0' '1' '?' ,问号可随意 ),要求将其转成格雷码,给一个序列a,若转成的格雷码第i位为1,则得分+a[i].求填充问号使得得分最多. 思路:如果了 ...
- [leetcode]Gray Code @ Python
原题地址:https://oj.leetcode.com/problems/gray-code/ 题意: The gray code is a binary numeral system where ...
- LeetCode——Gray Code
Description: The gray code is a binary numeral system where two successive values differ in only one ...
- LeetCode: Gray Code [089]
[题目] The gray code is a binary numeral system where two successive values differ in only one bit. Gi ...
随机推荐
- Hibernate之Criteria的完整用法
Criteria的完整用法 QBE (Query By Example) Criteria cri = session.createCriteria(Student.class); cri.add(E ...
- Invisible or disabled control cannot be activated
在WPF 应用程序下出现:Invisible or disabled control cannot be activated(不见的或禁用的控件不能被激活)错误. System.ArgumentExc ...
- mybatis java.lang.UnsupportedOperationException
mybatis抛出下面的异常: org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exc ...
- cocos2d-x之action初试
bool HelloWorld::init() { if ( !Layer::init() ) { return false; } Size visibleSize = Director::getIn ...
- Eclipse 启动Tomcat 超时报错的解决方案
在用eclipse开发项目 用tomcat发布项目的时候 会提示超时, Server Tomcat v7.0 Server at localhost was unable to start wit ...
- 05_最长公共子序列问题(LCS)
问题来源:刘汝佳<算法竞赛入门经典--训练指南> P60 问题7: 问题描述:给两个子序列A和B,求长度最大的公共子序列.比如1,5,2,6,8,和2,3,5,6,9,8,4的最长公共子序 ...
- 记一次linux服务器问题处理过程
本周二的时候,涛哥找我,说明了一件事,在安装ganglia的时候,发生的一个问题. 在一台suse 10 sp1的服务器上,安装ganglia的一个依赖包,libconfuse.rpm,安装完成之后, ...
- JVM的内存区域划分
JVM的内存区域划分 学过C语言的朋友都知道C编译器在划分内存区域的时候经常将管理的区域划分为数据段和代码段,数据段包括堆.栈以及静态数据区.那么在Java语言当中,内存又是如何划分的 ...
- Docker与容器快速入门
Docker之风席卷全球,但很多人觉得docker入门确实不太容易,其原因在于很多知识点上没准备好,在docker解决了什么问题.怎么解决的.用什么技术解决的都还没想清楚的时候就去探索docker组件 ...
- hdu1042 N!
/* N! Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) Total Subm ...