题目描述:

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.

考察位操作

从题目中例子可以发现这么一个规律:

假设结果数组为B,原始数组为A,则有:

B[i]  = A[i] ^ (A[i]>>).  

例如 : A[] =
B[] = ^(>>)
= ^
= =

解法如下:

 class Solution {
public:
vector<int> grayCode(int n) {
int len = pow(2.0,n),i,a;
vector<int> vi;
for(i=;i<len;i++)
{
a= i^(i>>);
vi.push_back(a);
}
return vi;
}
};

[LeetCode 题解]:Gray Code的更多相关文章

  1. 【题解】【排列组合】【回溯】【Leetcode】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. 【LeetCode】Gray Code

    Gray Code The gray code is a binary numeral system where two successive values differ in only one bi ...

  4. 【leetcode】Gray Code (middle)

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

  5. leetcode[88] Gray Code

    题目:格雷码. 格雷码是从0开始且之后两个相邻码之间只有一个符号不相同,例如000,100,101,111三个相邻之间只有一个二进制不同. 现在给定一个数字n,然后给出格雷码所对应的数字.例如: Fo ...

  6. Java for LeetCode 089 Gray Code

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

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

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

  8. LeetCode题目:Gray Code

    原题地址:https://leetcode.com/problems/gray-code/ class Solution { public: vector<int> grayCode(in ...

  9. Leetcode#89 Gray Code

    原题地址 二进制码 -> 格雷码:从最右边起,依次与左边相邻位异或,最左边一位不变.例如: 二进制: 1 0 0 1 1 1 0 |\|\|\|\|\|\| 格雷码: 1 1 0 1 0 0 1 ...

  10. [LeetCode]题解(python):089 Gray Code

    题目来源 https://leetcode.com/problems/gray-code/ The gray code is a binary numeral system where two suc ...

随机推荐

  1. 聚合模型---K-Means

    聚类模型:K-Means 聚类(clustering)属于无监督学习(unsupervised learning) 无类别标记 在线 demo:http://syskall.com/kmeans.js ...

  2. 关于C++成员函数指针的使用

    在做项目的时候,遇到了在类中根据不同的调用函数,在被调用函数的某处需要做不同的处理,本来就想着直接在类中设个标记变量判断下就好了,不过觉得这样代码可能看起来会有些凌乱,而且效率估计有些低,于是想起来使 ...

  3. 36种漂亮的CSS3网页按钮Button样式

    <!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8& ...

  4. python学习——练习题(9)

    """ 题目:暂停一秒输出. 程序分析:使用 time 模块的 sleep() 函数. http://www.runoob.com/python/python-date- ...

  5. python拷贝目录下的文件

    #!/usr/bin/env python # Version = 3.5.2 import shutil base_dir = '/data/media/' file = '/backup/temp ...

  6. sublime +react+es6开发环境

    Babel Sublime3才有的插件,支持ES6.JSX语法高亮. 菜单->View->Syntax->Open all with current extension as...- ...

  7. Coursera连接不上(视频无法播放),修改hosts文件

    视频问题 如果Coursera网站连接不上,或者视频加载不出来.可以通过如下方式进行配置:   一.找到hosts文件 Windows 系统, hosts文件位于: [C:\Windows\Syste ...

  8. Makefile 自动搜索 c 和 cpp 文件, 并生成 .a 静态库文件

    最近 又弄linux 下的 .a 静态库编译, 于是想 做个 一劳永逸的Makefile, 经过一番折腾, 最后成功了 只需要 改两个 参数 就可以执行了(MYLIB 和 VPATH), 代码 如下: ...

  9. 88. Merge Sorted Array (Array)

    Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: Y ...

  10. linux系统中的命令替换与整数运算$(),$(())

    一.$()与`` 在 bash shell 中,$( ) 与 ` ` (反引号) 都是用来做命令替换(command substitution)用的. 所谓的命令替换与我们第五章学过的变量替换差不多, ...