【题目】

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. Python 动态加载并下载"梨视频"短视频

    下载链接:http://www.pearvideo.com/category_1 import requests from lxml import etree import re from urlli ...

  2. logging 日志

    1. 四步: import logging #初始化 logger = logging.getLogger("log_name") #设置级别 logger.setLevel(lo ...

  3. python编码问题在此终结

     转载:https://www.cnblogs.com/whatisfantasy/p/6422028.html 1 版本差异概览 1.1 Python 2.X: str(用于8位文本和二进制数据) ...

  4. FTP传输协议的应用详解

    FTP的目标:1)促进程序.数据文件按的共享;2)鼓励使用远程计算机;3)使用户不必面对不同主机上不同文件系统的差异;4)对数据进行高效可靠的传输FTP的作用:就是让用户连接上一个远程计算机,察看远程 ...

  5. soul

    mark,数据使用gzip加密,因为公司网络卡,明天后天看.

  6. Vagrant 管理部署 VirtualBox (推荐使用)

    学习一段时间的大数据和容器技术,使用虚拟机搭建实验环境还是挺耗时耗力的. 一旦虚拟机坏掉了,还要重新开始. 最近发现了Vagrant, 简直好用上天,方便快捷,易用. 下面介绍如何在Windows中安 ...

  7. shell sort result to self

    You can use file redirection to redirected the sorted output: sort input-file > output_file Or yo ...

  8. shell编程第四天

  9. BZOJ.2324.[ZJOI2011]营救皮卡丘(费用流 Floyd)

    BZOJ 洛谷 首先预处理出\(dis[i][j]\),表示从\(i\)到\(j\)的最短路.可以用\(Floyd\)处理. 注意\(i,j\)是没有大小关系限制的(\(i>j\)的\(dis[ ...

  10. 【DWM1000】 code 解密6一TAG 状态机第一步

    我们前面分析过,不论ANCHOR 还是TAG,前面变量的初始化基本都是一样的,只是状态机必须明确区分不同的设备类型.我们从开始看TAG.由于初始化TAG的 testAppState一样初始化为TA_I ...