一天一道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. sublime text3中设置Emmet输入标签自动闭合

    项目后端前一段时间从C#转成了JAVA,在开发的过程中,由于HTML对标签的语法很宽松,比如这样:<img src="" alt="">在标签的结尾 ...

  2. Mysql索引介绍及常见索引的区别

    关于MySQL索引的好处,如果正确合理设计并且使用索引的MySQL是一辆兰博基尼的话,那么没有设计和使用索引的MySQL就是一个人力三轮车.对于没有索引的表,单表查询可能几十万数据就是瓶颈,而通常大型 ...

  3. HTML入门知识

    B/S:浏览器-服务器 C/S:客户端-服务器 更新麻烦 管理麻烦 PHP:基于BS结构进行项目开发的语言 ====================HTML:超文本标记语言 -- 控制网面内容CSS: ...

  4. vue项目开发中遇到的问题总结--内部分享

     1.路由变化页面数据不刷新问题 这种情况一般出现在vue-router的history模式下,初次进入会执行钩子函数,再次进入时则不会. 解决方案: 监听路由变化 watch : { "$ ...

  5. AnyConnect使用说明(手机版)

    一.下载安装客户端 iPhone手机在App Store 里搜索 “Anyconnect”下载安装. Android手机需另外下载Anyconnect. 二. 1.打开AnyConnect,点击&qu ...

  6. ACM Bee

    In Africa there is a very special species of bee. Every year, the female bees of such species give b ...

  7. Docker的Etcd项目

    etcd 是 CoreOS 团队发起的一个管理配置信息和服务发现(service discovery)的项目,在这一章里面,我们将介绍该项目的目标,安装和使用,以及实现的技术. Docker的etcd ...

  8. CSDN没有审核投诉的真实性直接删除博主上传的资源

      今天打开博客,发现一条未读通知:您上传的资源* * * *因质量投诉没有通过审核,如有疑问,请联系webmaster@csdn.net 我马上去看了下我的资源下载页,资源已经被删除,积分也已清空- ...

  9. iOS中的NSURLProtocol

    转自:iOS知识小集 NSURLProtocol类(注意,这个不是协议)经常用于实现一些URL Loading System相关的黑魔法.它可以拦截URL Loading System相关的网络请求, ...

  10. Markdown对应Yelee主题语法

    概述 这里说的是Yelee主题的语法和原生语法是有些区别的:更多的基础语法可以到Cmd Markdown上面去查看:但是我觉得都会各有不同吧 注意这里说的不是真正意义上的Markdown语法 标题 一 ...