题目

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.

解答

这就是离散数学里面一个格雷码的快速推导算法。。。

比如2位格雷码是00 01 11 10,这个数列奇数项左移后先加0再加1,偶数项左移后先加1再加0,按顺序组成的数列就是3位格雷码000 001 011 010 110 111 101 100。原理很简单,因为本来就是两两相邻之间差一位,现在按照这样操作,左移扩展一位,而新加的这一位又是01100110,这样原来每个数都扩展为相邻差一位,而原来相邻差一位的两个数之间加的是同样的一位数。

下面是AC的代码:

class Solution {
public:
    vector<int> grayCode(int n) {
        vector<int> ans;
        vector<int> temp;
        ans.push_back(0);
        for(int i = 0; i < n; i++){
            int length = ans.size();
            temp.clear();
            for(int i = 0; i < length; i++){
                if(i % 2 == 0){
                    temp.push_back(ans[i] * 2 + 0);
                    temp.push_back(ans[i] * 2 + 1);
                }
                else{
                    temp.push_back(ans[i] * 2 + 1);
                    temp.push_back(ans[i] * 2 + 0);
                }
            }
            ans = temp;
        }
        return ans;
    }
};

119

LeetCode OJ 89. Gray Code的更多相关文章

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

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

  2. 【LeetCode】89. Gray Code 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  3. 【LeetCode】89. Gray Code

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

  4. 【LeetCode】89. Gray Code (2 solutions)

    Gray Code The gray code is a binary numeral system where two successive values differ in only one bi ...

  5. LeetCode OJ:Gray Code(格林码)

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

  6. 89. Gray Code - LeetCode

    Question 89. Gray Code Solution 思路: n = 0 0 n = 1 0 1 n = 2 00 01 10 11 n = 3 000 001 010 011 100 10 ...

  7. [LeetCode] 89. Gray Code 格雷码

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

  8. leetCode 89.Gray Code (格雷码) 解题思路和方法

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

  9. 89. Gray Code

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

随机推荐

  1. MySQL 之 mysqlbinlog解析binlog乱码问题解密

    发现mysql库的binlog日志出来都是乱码,如下所示: BINLOG ’ IXZqVhNIAAAALQAAAGcBAAAAAHoAAAAAAAEABHRlc3QAAno0AAEDAABUOcnY ...

  2. BCGcontrolBar(七) 添加仪表盘、动态图表显示等控件

    BCG的 BCGPGaugesDemo有众多仪表盘控件可以参考使用 编写时同ListCtrl一样 在停靠面板上加入仪表盘和动态曲线 主要代码 //插入CPU图形 pContainer->SetF ...

  3. strut2的核心知识和工作原理

    http://blog.csdn.net/laner0515/article/details/27692673/     写的很详细

  4. 思考-Status management and validation(状态管理与验证)

    结合自己的项目,有这么一个模块,这个模块用来添加一个停车场,注册信息又分:基本信息,管理设置,管理员设置3部分组成,每部分都有input=text的输入框,点击保存按钮需要验证各个部分的输入框是否有合 ...

  5. 自然语言处理领域重要论文&资源全索引

    自然语言处理(NLP)是人工智能研究中极具挑战的一个分支.随着深度学习等技术的引入,NLP领域正在以前所未有的速度向前发展.但对于初学者来说,这一领域目前有哪些研究和资源是必读的?最近,Kyubyon ...

  6. Tomcat+Nginx+Redis+MySQL实现反向代理、负载均衡、session共享

    一.环境准备 时间同步 关闭防火墙 联通网络,配置yum源 软件包链接:https://pan.baidu.com/s/1qYbtpnQ 二.安装nginx 1.解决依赖关系 [root@nginx- ...

  7. IntelliJ IDEA 编译代码报错 找不到符号 符号: 找不到符号包 包

    在使用IDEA的时候,经常出现过找不到包或者找不到符号的情况,可以尝试以下几种方式来解决 1.如果项目使用的是Maven可以使用Maven-Reimport 2.还可以 Invalidate and ...

  8. GPUImage中对比度调整的实现——GPUImageContrastFilter

    对比度指的是一幅图像中明暗区域最亮的白和最暗的黑之间不同亮度层级的测量,差异范围越大代表对比越大,图像越鲜亮,差异范围越小代表对比越小,图像越灰. GPUImage中实现了对比度调整的Filter,其 ...

  9. Python3里查看某一元素的源码(检查元素定位是否准确)

    #coding:utf-8 #显示网页元素的HTML源码from selenium import webdriver driver = webdriver.Ie()driver.implicitly_ ...

  10. Android xml 绘制图形

    一般用shape定义的xml文件存放在drawable目录下,若项目没有该目录则新建一个,而不要将它放到drawable-hdpi等目录中. 使用shape可以自定义形状,可以定义下面四种类型的形状, ...