题目描述:

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. Linux Centos 7 RabbitMQ 安装

    下载地址:http://www.rabbitmq.com/releases/rabbitmq-server/ 找到rabbitmq-server-3.6.15-1.el7.noarch.rpm 第一步 ...

  2. IDEA2018.2版本注册

    IntelliJ IDEA 2018.2版本注册 1.到官网下载IDEA安装文件,windows版本ideaIU-2018.2.2.exe,然后安装: 2.下载补丁包JetbrainsCrack-3. ...

  3. Asp.Net 自定义 httpmodel 中间件 管道

    https://msdn.microsoft.com/en-us/library/aa719858(v=vs.71).aspx http://www.cnblogs.com/jimmyzhang/ar ...

  4. Git 版本导致 clone 故障

    问题描述: git clone 报错如下: Initialized empty Git repository in /root/project_php/.git/ error: The request ...

  5. linux 下的mysql 连接报错

    报错: Fri Jul 28 16:28:52 CST 2017 WARN: Establishing SSL connection without server’s identity verific ...

  6. Visual Studio Find All no results.

    重装WDK什么的有时候有bug....写下面注册表修复 Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\Wow6432Node\CLSI ...

  7. Specular Mask Texture

    [Specular Mask Texture] Specular Texture是一个Mask纹理,通过Mask纹理的texel可以控制每个像素的纹理. 在属性中添加Specular Mask Tex ...

  8. 高性能Web服务器Nginx的配置与部署研究(11)应用模块之Memcached模块的两大应用场景

    一.应用场景1 最近在一个项目中,用到了Nginx的Memcached模块,所以就在这个系列教程中提前把Memcached模块拿出来写了.另外发现最近我的 博客文章频频被很多用采集器的网站拿走,帮我发 ...

  9. CS API 测试2

    //删除数据中心 http://192.168.150.16:8900/client/api?command=deleteZone&id=c2d4f46a-51af-4806-8378-4b3 ...

  10. code2039 骑马修栏杆

    欧拉通路: find_circuit(结点i){ 当结点i有邻居时 选择任意一个邻居j: 删除边(i,j)或者做访问标记: find_circuit(结点j): 输出或存储节点i: } 本题注意点的编 ...