LeetCode(87) 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.
分析
二进制串值与格雷码值转换问题,掌握两者之间的关系,则此题便很简单。
二进制码 ——> 格雷码 称为 编码问题:
此方法从对应的n位二进制码字中直接得到n位格雷码码字,步骤如下:
(1)对n位二进制的码字,从右到左,以0到n-1编号
(2)如果二进制码字的第i位和i+1位相同,则对应的格雷码的第i位为0,否则为1(当i+1=n时,二进制码字的第n位被认为是0,即第n-1位不变)—— 异或规则
如:
Binary Code :1011 要转换成Gray Code
1011 = 1(照写第一位), 1(第一位与第二位异或 1^0 = 1), 1(第二位异或第三位, 0^1=1), 0 (1^1 =0) = 1110
其实就等于 (1011 >> 1) ^ 1011 = 1110
格雷码 ——> 二进制码 称为 解码问题
从左边第二位起,将每位与左边一位解码后的值异或,作为该位解码后的值(最左边一位依然不变)。依次异或,直到最低位。依次异或转换后的值(二进制数)就是格雷码转换后二进制码的值。
AC代码
class Solution {
public:
vector<int> grayCode(int n) {
//n位构成的二进制序列值为0~2^n-1
if (n == 0)
return vector<int>(1,0);
//1左移n位 得到2^n
int size = 1 << n;
vector<int> ret;
for (int i = 0; i < size; i++)
{
ret.push_back(i ^ (i >> 1));
}
return ret;
}
};
LeetCode(87) Gray Code的更多相关文章
- LeetCode(87):扰乱字符串
Hard! 题目描述: 给定一个字符串 s1,我们可以把它递归地分割成两个非空子字符串,从而将其表示为二叉树. 下图是字符串 s1 = "great" 的一种可能的表示形式. gr ...
- 编码规范(二)之Code Templates的设置(转)
http://swiftlet.net/archives/1199 编码规范(二)之Code Templates的设置(转) 文件(Files)注释标签:/** * @Title: ${file_na ...
- 编码规范(一)之Code Templates的设置(转)
编码规范(一)之Code Templates的设置 基于公司的主流开发工具为eclipse,但每个人都有自己的编码习惯,为了统一格式,这里通过三个方面:设置Code Templates.Checkst ...
- (译)iOS Code Signing: 解惑
子龙山人 Learning,Sharing,Improving! (译)iOS Code Signing: 解惑 免责申明(必读!):本博客提供的所有教程的翻译原稿均来自于互联网,仅供学习交流之用,切 ...
- LeetCode(275)H-Index II
题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...
- LeetCode(220) Contains Duplicate III
题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...
- LeetCode(154) Find Minimum in Rotated Sorted Array II
题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...
- LeetCode(122) Best Time to Buy and Sell Stock II
题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...
- LeetCode(116) Populating Next Right Pointers in Each Node
题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...
随机推荐
- iOS 监测电话呼入
1.首先引入CoreTelephony框架,代码里: @import CoreTelephony; 项目设置里: 2.定义属性,建立强引用: @property (nonatomic, strong) ...
- python之文件的读写
读 r 读写模式 r+ 如果打开文件时没有指定模式,默认只读,如果使用r或r+,文件不存在时会报错 写 w 写读模式 w+ w模式会清空原有的文件内容 追加 a 追加读 ...
- 一个Nice的生活主题博客模板
https://www.bitcron.com/ https://api.bitcron.com/ https://chopstack.com/
- 在HTML页面中实时获取新消息的方法 “JavaScript中的setInterval用法”
JavaScript中的setInterval用法(资料来源:博主---八神吻你 ) setInterval动作的作用是在播放动画的时,每隔一定时间就调用函数,方法或对象.可以使用本动作更新来自数 ...
- vs2010 坑爹的BUG
以前用VS2005的时候,就遇到过一些很奇怪的BUG,比如始终报错,然后把项目文件删除,重新创建一个项目文件,就好了. 今天用VS2010测试程序时,又发现一个坑爹的BUG,这绝对不是我的错! sta ...
- EJB Timer Service is not available. Timers for application with id 95795415990861824 will not be deleted
delete follows:glassfish\domains\domain1\applications\ejb-timer-service-appglassfish\domains\domain1 ...
- 74LVC2G241双缓冲3态驱动器
- F. Anton and School 位运算 + 化简
http://codeforces.com/contest/734/problem/F 因为 x + y = (x & y) + (x | y) 有了这个公式后,然后应该手动模拟一下,把公式化 ...
- Spark MLlib编程API入门系列之特征选择之R模型公式(RFormula)
不多说,直接上干货! 特征选择里,常见的有:VectorSlicer(向量选择) RFormula(R模型公式) ChiSqSelector(卡方特征选择). RFormula用于将数据中的字段通过R ...
- OCP 11g 第一章练习
练习 1-1 研究所在环境的DBMS 这是一个书面练习,没有具体的解决方案. 确定自己所在环境使用的应用程序, 应用服务器 , 和数据库. 然后集中精力研究数据库, 体验一下数据库的规模和忙碌程度. ...