题目

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;
}
};

GitHub测试程序源码

LeetCode(87) Gray Code的更多相关文章

  1. LeetCode(87):扰乱字符串

    Hard! 题目描述: 给定一个字符串 s1,我们可以把它递归地分割成两个非空子字符串,从而将其表示为二叉树. 下图是字符串 s1 = "great" 的一种可能的表示形式. gr ...

  2. 编码规范(二)之Code Templates的设置(转)

    http://swiftlet.net/archives/1199 编码规范(二)之Code Templates的设置(转) 文件(Files)注释标签:/** * @Title: ${file_na ...

  3. 编码规范(一)之Code Templates的设置(转)

    编码规范(一)之Code Templates的设置 基于公司的主流开发工具为eclipse,但每个人都有自己的编码习惯,为了统一格式,这里通过三个方面:设置Code Templates.Checkst ...

  4. (译)iOS Code Signing: 解惑

    子龙山人 Learning,Sharing,Improving! (译)iOS Code Signing: 解惑 免责申明(必读!):本博客提供的所有教程的翻译原稿均来自于互联网,仅供学习交流之用,切 ...

  5. LeetCode(275)H-Index II

    题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...

  6. LeetCode(220) Contains Duplicate III

    题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...

  7. LeetCode(154) Find Minimum in Rotated Sorted Array II

    题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...

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

  9. LeetCode(116) Populating Next Right Pointers in Each Node

    题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...

随机推荐

  1. Ubuntu 18.04 LTS 常见问题解决 2

    1 每次重启后都有system program problem detected sudo gedit /etc/default/apport 然后将打开的文件中的enabled=1改为0,原因暂时不 ...

  2. iOS UITableView ExpandableHeader(可形变的Header)

    最常见的header就是在tableView下拉时header里的图片会放大的那种, 最近研究了一下,自己实现了这种header. 1.设置TableView的contentInset(为header ...

  3. spring 通配符

    原文地址:http://www.bubuko.com/infodetail-848675.html classpath是指 WEB-INF文件夹下的classes目录(惯例大于配置) classpat ...

  4. 洛谷p2234/BZOJ1588 [HNOI2002]营业额统计

    题目链接: 洛谷 BZOJ 分析: 好像没有什么好说的就是一个平衡树的板子--唯一要注意的就是这里要找的并不是严格的前驱和后继,因为如果找到之前某一天的营业额和它相等那么差就是0,所以我们仍然在结构体 ...

  5. Odd sum CodeForces - 797B

    Odd sum CodeForces - 797B 好方法:贪心 贪心2 糟糕(不用动脑)的方法:dp ans[i][0]表示到第i个和为偶数最大,ans[i][1]表示到第i个和为奇数最大. 但是, ...

  6. Contextual Action bar(1) CAB in Android

    Contextual Action bar (CAB) in Android BY PARESH MAYANI - OCTOBER, 23RD 2013 Before getting into the ...

  7. 488 Zuma Game 祖玛游戏

    回忆一下祖玛游戏.现在桌上有一串球,颜色有红色(R),黄色(Y),蓝色(B),绿色(G),还有白色(W). 现在你手里也有几个球.每一次,你可以从手里的球选一个,然后把这个球插入到一串球中的某个位置上 ...

  8. pyspark中使用累加器Accumulator统计指标

    评价分类模型的性能时需要用到以下四个指标 最开始使用以下代码计算,发现代码需要跑近一个小时,而且这一个小时都花在这四行代码上 # evaluate model TP = labelAndPreds.f ...

  9. js dom node.children与node.childNodes区别

    不同点:node.children不会取到节点下面的TextNode但是node.childNodes会取到 共同点:两者都是集合类数组,可以通过索引的方式取到值也可以用for循环遍历

  10. JSP和Servlet性能优化经验谈

    你的J2EE应用是不是运行的很慢?它们能不能承受住不断上升的访问量?本文讲述了开发高性能.高弹性的JSP页面和Servlet的性能优化技术.其意思是建立尽可能快的并能适应数量增长的用户及其请求.在本文 ...