题目

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. Eclipse中Activiti插件的安装

    要想使用Activiti流程引擎,需要在Eclipse安装Activiti插件,才能画流程设计图. 打开Eclipse,点击help -> Install new Software 然后点击 A ...

  2. 【Unix网络编程】chapter3套接字编程简介

    chapter3套接字编程简介3.1 概述 地址转换函数在地址的文本表达和他们存放在套接字地址结构中的二进制值之间进行转换.多数现存的IPv4代码使用inet_addr和inet_ntoa这两个函数, ...

  3. flume用场景及架构原理

    Flume是什么 1.flume可以将采集到的数据存储到HDFS上,也可以放在Hbase上. 2.flume就是一个中间插件,他的作用就是屏蔽数据源和数据存储系统的差异.可以在不同的数据源采集数据,因 ...

  4. day4作业(基本运算流程if for)

    #coding:utf-8'''默写: 循环嵌套 必做: 1. 求1-100的所有数的和 2. 输出 1-100 内的所有奇数 3. 输出 1-100 内的所有偶数 5. 求1-2+3-4+5 ... ...

  5. flask 之cbv ,flash闪现,Flask_Session,WTForms - MoudelForm

    1.CBV : from flask import views  class LoginView(views.MethodView):  def get(self):   return     def ...

  6. django, tornado

    django  由多线程写的 tornaod  由epoll机制

  7. ElasticSearch 2 (1) - Getting Start

    ElasticSearch 2.1.1 (1) - Getting Start Install & Up cd elasticsearch-2.1.1/bin ./elasticsearch ...

  8. ROS Industrial 简介

    ROS_I means ROS Industrial ROS_I 解决了哪些问题: 1. 让自动化可以互相协作,操纵器.末端执行器.感知系统/传感器,移动平台,周边设备,都可只用一种语言(ROS me ...

  9. uva-11054-模拟

    题意:一条街有许多卖酒的店,有些店需要进酒,有些店需要出酒,所有的店的需求总和为0,问怎么移动,使酒的移动总和最少. 模拟,考虑一家店的需求为xi,无论是从左或者是从右或者其他形式,让xi->0 ...

  10. 关于微信支付接口,curl错误代码58

    微信支付接口,curl错误代码58 之前的微信付款到用户零钱都是好好的,今天运营来找我, 我想了了下,就是进行了网站搬家 看了下 微信支付相关的证书配置文件 知道了,在这个 要改下证书的路径 WxPa ...