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. php实现MySQL两库对比升级版

    define('DATABASE1', 'db1'); $dbi1 = new DbMysql; $dbi1->dbh = 'mysql://root:password@127.0.0.1/'. ...

  2. 提升HTML5的性能体验系列之四 使用原生UI

    原生UI的设计目的 HTML和css有一个优势就是灵活的样式设计.在大多数情况下,我们都应该使用HTML+css来负责UI.但是有些情况下,我们发现HTML+css的UI不满足需求.1. 绝对置顶HT ...

  3. Globalization and accessibility for tile and toast notifications (Windows Store apps)

    http://msdn.microsoft.com/en-us/library/windows/apps/hh831183.aspx 做 HighContrast时,采用以下分目录方式: /Proje ...

  4. O365 Manager Plus详解

  5. MySQL Server参数优化 - innodb_file_per_table(独立表空间)

    1 简介    Innodb存储引擎可将所有数据存放于ibdata*的共享表空间,也可将每张表存放于独立的.ibd文件的独立表空间.    共享表空间以及独立表空间都是针对数据的存储方式而言的.    ...

  6. kbmmw 中JSON 中使用SQL 查询

    前面讲到了kbmmw 的JSON 对象操作,如何快速的查找JSON 中的值? 一种办法就是通过遍历的方法,其实在kbmmw 还有一种灵活的查询方式, 就是通过SQL 方式查询JSON 中的值.也就是说 ...

  7. layer 弹框不显示内容

    // layer的弹框不显示信息 可能是背景颜色和字体颜色冲突 改下字体颜色即可 layer.msg('<p style="color:black">用户名不能为空&l ...

  8. js 获取数组最后一个元素

    当然有很多中做法 我这边就随便写几个最常用 最简单的方法把 # shift 删除数组第一个元素,并返回该元素,跟pop差不多 var a = ["aa","bb" ...

  9. Oracle常用命令-用户、表空间、赋权限、导入导出

    1.1   删除表空间 drop tablespace QBKJ including contents and datafiles; 1.2   删除用户 drop user admin cascad ...

  10. php mysqli 链接数据库 CURD 增改查删

    <?php function println($msg) { echo "<br>"; echo $msg; } $mysql_server_name = &qu ...