一天一道LeetCode

本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github

欢迎大家关注我的新浪微博,我的新浪微博

欢迎转载,转载请注明出处

(一)题目

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.

(二)解题

题目大意:给定一个整数n,生成n位的格雷码。

之前做过格雷码的题目,每次都在n-1位格雷码的后面加上0和1组成n位格雷码。

题目中提到只认示例中给定的格雷码顺序。

因此,很容易找到规律,每次从n-1位格雷码的尾部往前,依次加上2的n-1次方,就得到新的n位格雷码。

代码如下:

class Solution {
public:
    vector<int> grayCode(int n) {
        vector<int> ret;
        if(n==0) {//为0的时候需要特殊处理
            ret.push_back(0);
            return ret;
        }
        for(int i = 0 ; i < n ; i++)
        {
            if(i==0)//这里表示1位格雷码
            {
                ret.push_back(0);
                ret.push_back(1);
            }
            else
            {
                for(int j = ret.size()-1 ; j >=0 ; j--)//每次从n-1位格雷码的尾部向前,一次加上2的n-1次方得到新的格雷码
                {
                    ret.push_back(pow(2,i)+ret[j]);
                }
            }
        }
        return ret;
    }
};

【一天一道LeetCode】#89. Gray Code的更多相关文章

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

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

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

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

  3. Leetcode#89 Gray Code

    原题地址 二进制码 -> 格雷码:从最右边起,依次与左边相邻位异或,最左边一位不变.例如: 二进制: 1 0 0 1 1 1 0 |\|\|\|\|\|\| 格雷码: 1 1 0 1 0 0 1 ...

  4. 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 ...

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

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

  6. 【LeetCode】89. Gray Code

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

  7. LeetCode OJ 89. Gray Code

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

  8. 【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 ...

  9. 89. Gray Code

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

随机推荐

  1. idea和androidstudio的首次git配置一些问题

    网上都有很清楚的步骤 但是 都是教怎么使用 但是对第一次应用idea内部vcs的git 则很少有详细说明 首先要在网上创建个项目 然后本地git clone下来 不建议内部vcs的fetch from ...

  2. C语言 分支与循环 递推思想 穷举 流程的转移控制

    条件语句 开关控制语句(SWITCH语句) 象坐电梯一样,break是按的楼层,不加break则会一直执行下去. 上面程序有细节BUG,边界测试输入-5,105时由于整除会得到错误的结果. 解决方法: ...

  3. SpringBoot学习之启动探究

    SpringApplication是SpringBoot的启动程序,我们通过它的run方法可以快速启动一个SpringBoot应用.可是这里面到底发生了什么?它是处于什么样的机制简化我们程序启动的?接 ...

  4. 使用CSS让多出来的字变为省略号

    <style> .text1 { width:200px; overflow:hidden; text-overflow:ellipsis; -o-text-overflow:ellips ...

  5. discuz全新安装升级,导入旧数据过程,顺便gbk转utf8

    由于discuz官方已经不更新了,现在又只有现成的utf8版本,没有gbk版本.我们原来使用的是gbk编码的,最近想改版,顺便升级一下,就索性把gbk也换成utf8吧,这样以后也方便,国际化嘛! 第一 ...

  6. 使用python scipy.optimize linprog和lingo线性规划求解最大值,最小值(运筹学学习笔记)

    1.线性规划模型: 2.使用python scipy.optimize linprog求解模型最优解: 在这里我们用到scipy中的linprog进行求解,linprog的用法见https://doc ...

  7. 一起撸个简单粗暴的Tv应用主界面的网格布局控件(上)

    这一篇是真的隔了好久了~~,也终于可以喘口气来好好写博客了,这段时间实在是忙不过来了,迭代太紧.好,废话不多说,进入今天的主题. 效果 图一是Tv应用:当贝市场的主页 图二是咱自己撸的简单粗暴的 Tv ...

  8. LintCode题解之统计数字

    直接硬搜就可以了,只是需要考虑k为0的情况. public class Solution { /* * @param : An integer * @param : An integer * @ret ...

  9. Spring Security基于Java配置

    Maven依赖 <dependencies> <!-- ... other dependency elements ... --> <dependency> < ...

  10. Windows下使用Vim极简入门

    0.下载与安装 在vim官网下载 1.Vim常见的几种模式: 一般模式:主要用于浏览,不能随意删除.修改等.按Esc进入该模式. 插入模式:类似平常我们打开记事本后所在的模式.在命令模式下按i进入. ...