【题目】

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. 20165235 祁瑛 2018-4 《Java程序设计》第九周学习总结

    20165235 祁瑛 2018-4 <Java程序设计>第九周学习总结 教材学习内容总结 URL类 UR类是java.net包中的一个重要类,使用URL创建的对象的应用程序称作称作客户端 ...

  2. MarkdownPad 2在win10上安装及破解(含安装包)

    MarkdownPad 2 是一款较不错的Markdown编辑器,可快速将文本转换为美观的HTML/XHTML的网页格式代码,且操作方便,用户可以通过键盘快捷键和工具栏按钮来使用或者移除Markdow ...

  3. Emoji表情编解码库XXL-EMOJI

    <Emoji表情编解码库XXL-EMOJI> 一.简介 1.1 概述 XXL-EMOJI 是一个灵活可扩展的Emoji表情编解码库,可快速实现Emoji表情的编解码. 1.2 特性 1.简 ...

  4. Python 合并两个列表的多种方式,合并两个字典的多种方式

    一.合并列表 1.最简单的,使用+连接符: >>> a = [1,2,3] >>> b = [7,8,9] >>> a + b [1, 2, 3, ...

  5. LoRaWAN 1.1 网络协议规范 - 5 MAC指令

    LoRaWAN 1.1 网络协议规范 LoRaWAN 1.1 版本封稿很久了也没有完整啃过一遍,最近边啃边翻译,趁着这个机会把它码下来. 如果觉得哪里有问题,欢迎留言斧正. 翻译不易,转载请申明出处和 ...

  6. c++ stod很慢

    C++ Convert String to Double Speed (There is also a string-to-int performance test.) A performance b ...

  7. [OC] 使用 cocoaPods 导入 AFNetworking

    AFNetworking的GitHub地址: https://github.com/AFNetworking/AFNetworking 假设我们建立了一个叫做AFNWlearning的工程. 1.打开 ...

  8. QtQuick自定义主题以及控件样式指引

    自定义控件样式 请在Qt帮助索引中输入Customizing a Control进行查看 不过实际用下来感觉除非你想自己实现一套效果复杂的UI或是创造一个全新控件,比如:给UI添加模糊.虚化等Shad ...

  9. Java 8 (二) 新的时间API

    新的时间API 一)时间线 Instant对象:表示时间轴上的一个点,原点为1970-1-1的午夜. Duration对象:表示一段时间. 注意Instant和Duration类都是final. 二) ...

  10. python基础一 ------如何获取多个字典相同的键

    需求: 足球赛第一场进去统计  {"A":3,"B":2,"C":1}足球赛第二场进去统计  {"A":3," ...