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.

根据题目中的意思,我们只要求出一组合适的解即可。正确答案非常精妙(我没有想出来。。。),充分利用位运算。看来对于位运算的知识我还是要再加强。

答案出处:http://www.2cto.com/kf/201305/215316.html

 class Solution {
public: vector<int> grayCode(int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<int> result;
int m=<<n;
for(int i=;i<m;i++)
{
result.push_back(i^(i>>));
}
return result; }
};

参考答案

思路:首先,1<<n 是用来计算2的n次方。i^(i>>1)是格雷码的计算方法,表示第i个数是几。好久不用都想不起来了。。。

[leetcode.com]算法题目 - Gray Code的更多相关文章

  1. LeetCode(87) Gray Code

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

  2. LeetCode题目:Gray Code

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

  3. [leetcode.com]算法题目 - Restore IP Addresses

    Given a string containing only digits, restore it by returning all possible valid IP address combina ...

  4. [leetcode.com]算法题目 - Jump Game

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  5. [leetcode.com]算法题目 - Maximum Subarray

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  6. [leetcode.com]算法题目 - Same Tree

    Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...

  7. [leetcode.com]算法题目 - Triangle

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  8. [leetcode.com]算法题目 - Length of Last Word

    Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...

  9. [leetcode.com]算法题目 - Sqrt(x)

    Implement int sqrt(int x). Compute and return the square root of x. class Solution { public: int sqr ...

随机推荐

  1. Android Studio 老提示adb问题

    Android Studio 老提示adb问题,restart后任然无解,最后发现某手机助手软件占用端口... 解决步骤: C:\Users\xxx>netstat -ano | findstr ...

  2. mysql之练习题4

    准备表: create table class(cid int primary key auto_increment, caption ) not null unique); INSERT into ...

  3. Python10/17-re模块/hashlib模块/logging模块

    import logging # 1.日志的级别# logging.debug("这是一个调试信息") # 10# logging.info("常规信息") # ...

  4. 2018.11.01 NOIP训练 递增数列(迭代加深)

    传送门 直接迭代加深搜索. 发现每次最多增加一倍,最少增加一,于是果断上下界剪枝. 代码

  5. 组合数C(n,m)的四种求解方法

    转自:文章 1.暴力求解 C(n,m)=n*(n-1)*...*(n-m+1)/m!,(n<=15): int CF(int n,int m) { ,i,j; ;i--) ans*=i; ;i- ...

  6. SQL语句之奇形怪状的冷门函数

    lag() over() ) OVER(ORDER BY C.column) FROM Table C; 第一条记录已经无法再取前一条记录,所以LAG()函数返回空. SQL为意思如下. LAG(C. ...

  7. 在vue中没有数据的渲染方法

    1.例如在评论区中,评论一般分为两种形式,一种是有评论,一种是没有评论, 用v-if进行判断,判断的是评论的长度,此时评论的数据应为数组 2.可以computed中记性计算后进行数据的返回在用v-if ...

  8. Eclipse错误: 找不到或无法加载主类或项目无法编译10种解决大法

    1.在src文件夹上点右键-Build Path-Use as Source Folder,重新进行编译,一切正常了.2.在Eclipse工程文件夹上点右键-Refresh,重新编译,一功OK(这个方 ...

  9. Perl语言入门

    Perl 是 Practical Extraction and Report Language 的缩写,可翻译为 "实用报表提取语言". Perl语法基础: (1)Perl程序由声 ...

  10. mmm和mmma的区别

    m:编译整个安卓系统 makes from the top of the tree mm:编译当前目录下的模块,当前目录下需要有Android.mk这个makefile文件,否则就往上找最近的Andr ...