题目

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. 13-001 ViewComponents IViewComponentActivator

    接口定义: /// <summary> /// Provides methods to activate an instantiated ViewComponent /// </su ...

  2. PDP context

    PDP context[edit] The packet data protocol (PDP; e.g., IP, X.25, FrameRelay) context is a data struc ...

  3. BCGcontrolBar(二) 改变RIBBON字体

    在xp系统下 BCGcontrolBar字体会发虚 这时候重新设置下字体就行了 在单文档的MainFram的onCreate中加入 CFont *font=m_wndRibbonBar.GetFont ...

  4. 1449 - The user specified as a definer ('test'@'%') does not exist

    最近在做一个项目,由于服务器切换,所以需要将原有服务器的mysql数据表以及存储过程导入到另一个服务器的mysql数据库中.导入完成之后以为一切是那么的简单,却没有想到总还是出现了一些莫名其妙的问题. ...

  5. javaweb项目中遇到的一些乱码问题

    在做javaweb项目时,我们经常会遇到一些乱码问题: 首先,确定一点思想:要想不乱码,你要保证编码一致就行了,即统一编码~ 其一,jsp等页面中的中文显示乱码(这里不只是说jsp文件,其它文件也有这 ...

  6. Java - 20 Java 继承

    Java 继承 继承是java面向对象编程技术的一块基石,因为它允许创建分等级层次的类.继承可以理解为一个对象从另一个对象获取属性的过程. 如果类A是类B的父类,而类B是类C的父类,我们也称C是A的子 ...

  7. virt-install详解

    man virt-install VIRT-INSTALL() Virtual Machine Manager VIRT-INSTALL() NAME virt-install - provision ...

  8. Apache服务器下phalcon项目报Mod-Rewrite is not enabled问题

    问题如图: 项目已经按照官网的教程修改了.htaccess文件,仍旧报此错误,判断可能是apache未添加mod_rewrite,通过查询资料,经以下两步解决此问题: 1.执行sudo a2enmod ...

  9. 将任何GitHub内的代码转为外部CDN网址

    rawgit.com 有时候在GitHub找到需要使用于网站的档案(例如:CSS.JavaScript),但没有提供CDN网址就必须自己手动下载.上传到主机上才能够使用,有点耗时又不方便,因为GitH ...

  10. Redis 简介(官方翻译)

    Redis是一个开源(基于BSD开源协议).内存型结构数据存储,可当做数据库.缓存.消息代理.它支持的数据结构有字符串.哈希表.列表.集合.可随机查询的有序集合.位图.基数统计.用于半径查询的地理位置 ...