【题目】

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.

【题意】

本题是有关格雷码转换的,知道二进制转换为格雷码的转换规则,本题就很easy了。

    给定二进制下的位数n, 要求得到格雷码序列,并输出每一个格雷码相应的十进制表示。相邻格雷码之间仅仅有一位不同。

对于输入的n,格雷码序列可能有多个,题目要求输出任一个序列就可以。

    序列必须以0開始。

【思路】

n位二进制可表示2^n个数。因此格雷码序列长度即为2^n

    我们仅仅需从小到大把二进制转换成相应的格雷码就可以。转换规则例如以下:

    如果二进制数为x, 则其相应的格雷码为x>>1^x

    即x和其本身向右移动一位后的结果做抑或运算。

    

    【注意。n=0是本题觉得它能表示一个值0】

【代码】

class Solution {
public:
vector<int> grayCode(int n) {
vector<int> result; int size=1<<n; //一共能够表示2^n个数
int x=0;
while(x<size){
result.push_back(x>>1^x); //转换成相应格雷码
x++;
}
return result;
}
};

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

  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]Gray Code @ Python

    原题地址:https://oj.leetcode.com/problems/gray-code/ 题意: The gray code is a binary numeral system where ...

  3. LeetCode——Gray Code

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

  4. LeetCode:Gray Code(格雷码)

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

  5. LeetCode: Gray Code 解题报告

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

  6. [转载]LeetCode: Gray Code

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

  7. Leetcode Gray Code

    题目的意思就是将十进制转换成格雷码 首先将二进制转换成格雷码 根据此图可以看出二进制的第i和第i+1位异或为格雷码的第i+1位,对于给定的十进制数x,其(x>>1)相当于二进制向右移动一位 ...

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

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

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

随机推荐

  1. PAT (Basic Level) Practise - 换个格式输出整数

    题目链接:https://www.patest.cn/contests/pat-b-practise/1006 1006. 换个格式输出整数 (15) 时间限制 400 ms 内存限制 65536 k ...

  2. day4 class work answer

    count=0 s = input("请输入内容:") # asd234fsdafa5456fsdaf1 while s: s=s.lstrip("abcdefghijk ...

  3. Maya Max python PySide集成 shiboken版本对应关系

    Maya_Max _python_PySide集成_shiboken版本对应关系 1.如何查看 Maya Max 集成的 Python版本: Maya:在 Maya 的安装目录下的 bin 文件夹中找 ...

  4. RF:RF实现根据乳腺肿瘤特征向量高精度(better)预测肿瘤的是恶性还是良性—Jason niu

    %RF:RF实现根据乳腺肿瘤特征向量高精度(better)预测肿瘤的是恶性还是良性 load data.mat a = randperm(569); Train = data(a(1:500),:); ...

  5. 搭建本地maven库(nexus服务器)

    第一步,下载https://www.sonatype.com/download-oss-sonatype 别下3.x版本,下2.x版本 第二步,解压,在bin目录下执行cmd命令,nexus inst ...

  6. POJ 2155 Matrix 【二维树状数组】(二维单点查询经典题)

    <题目链接> 题目大意: 给出一个初始值全为0的矩阵,对其进行两个操作. 1.给出一个子矩阵的左上角和右上角坐标,这两个坐标所代表的矩阵内0变成1,1变成0. 2.查询某个坐标的点的值. ...

  7. django+mongodb 内置用户控制

    0x01 项目:django2.1 数据库:mongodb 这是一个很蛋疼的组合 mongodb并非官方支持使用的数据库,这意味着要使用user group permissions等进行用户和权限控制 ...

  8. jmeter connection reset解决方法

    方法仅作参考: 1.修改HTTP请求下面的Impementation选项,改成HttpClient4 2.在user.properties文件内修改: hc.parameters.file=hc.pa ...

  9. BZOJ.4821.[SDOI2017]相关分析(线段树)

    BZOJ LOJ 洛谷 恶心的拆式子..然后就是要维护\(\sum x_i,\ \sum y_i,\ \sum x_iy_i,\ \sum x_i^2\). 操作三可以看成初始化一遍,然后同操作二. ...

  10. IDEA常用配置

    一.安装Activiti 1.File -> Settings -> Plugins -> 搜索actiBPM 2.解决中文乱码问题 修改IDEA的安装目录中的idea.exe.vm ...