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.

二进制码->格雷码(编码):从最右边一位起,依次将每一位与左边一位异或(XOR),作为对应格雷码该位的值,最左边一位不变(相当于左边是0);
格雷码->二进制码(解码):从左边第二位起,将每位与左边一位解码后的值异或,作为该位解码后的值(最左边一位依然不变)。

Gray Code 0 = 0, 下一项是toggle最右边的bit(LSB), 再下一项是toggle最右边值为 “1” bit的左边一个bit,然后重复。直到最右边值为 “1” 的bit在最左边了,结束。

  1. class Solution {
  2. public:
  3. vector<int> grayCode(int n) {
  4. // Start typing your C/C++ solution below
  5. // DO NOT write int main() function
  6. vector<int> result;
  7. int nSize = 1 << n;
  8. for (int i = 0; i < nSize; ++i)
  9. {
  10. result.push_back((i>>1)^i);
  11. }
  12. return result;
  13. }
  14. };

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

  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(格雷码)

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

  3. [leetcode]Gray Code @ Python

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

  4. LeetCode——Gray Code

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

  5. LeetCode: Gray Code [089]

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

  6. LeetCode: Gray Code 解题报告

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

  7. Leetcode Gray Code

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

  8. 【一天一道LeetCode】#89. Gray Code

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 The gra ...

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

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

随机推荐

  1. ubuntu安装conda

    https://blog.csdn.net/menghuanbeike/article/details/79138651 你需要前往Anaconda的官网看下目前的下载地址: https://www. ...

  2. Django 的 Form组件

    Django的Form主要具有一下几大功能: 生成HTML标签 验证用户数据(显示错误信息) HTML Form提交保留上次提交数据 初始化页面显示内容 Form类的使用: 1.定义规则: from ...

  3. India and China Origins---hdu5652(二分 + bfs)或者(并查集)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5652 题意: 很久以前,中国和印度之间并没有喜马拉雅山相隔,两国的文化交流很频繁.随着喜马拉雅山海拔逐 ...

  4. Instagram 架构分析笔记(转)

    原文:http://dbanotes.net/?s=Instagram+%E6%9E%B6%E6%9E%84%E5%88%86%E6%9E%90%E7%AC%94%E8%AE%B0 作者:冯大辉 In ...

  5. 评论抓取:Python爬取微信在APPStore上的评论内容及星级

    #完整程序如下: import requests import re def getHTMLText(url): try: r = requests.get(url) r.raise_for_stat ...

  6. SHFileOperation的用法

    //删除文件或者文件夹bool DeleteFile(char * lpszPath){SHFILEOPSTRUCT FileOp={0};FileOp.fFlags = FOF_ALLOWUNDO ...

  7. JavaWeb404排错的小技巧

    报这种错误,404后面什么都没有的话,就证明处理器映射器根据url找不到handler. 报这种错误,证明处理器映射器根据url找到了handler,转发的jsp页面找不到,说明jsp页面错了.

  8. LeetCode——Palindrome Number

    Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negativ ...

  9. 1040. 有几个PAT(25)

    原题: https://www.patest.cn/contests/pat-b-practise/1040 思路: 先给大家扔个测试PAPAATTPATTT, 人工查一下这段字符串能组成 34个PA ...

  10. 在线学习--online learning

    在线学习 online learning Online learning并不是一种模型,而是模型的训练方法.能够根据线上反馈数据,实时快速的进行模型调优,使得模型能够及时反映线上的变化,提高线上预测的 ...