leetcode_89_Gray Code

Gray Code

欢迎大家阅读参考,如有错误或疑问请留言纠正,谢谢

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/2)。

这题要求生成 n 比特的所有格雷码。

方法 1,最简单的方法,利用数学公式,对从 0 ~ 2 n ? 1 的所有整数,转化为格雷码。

方法 2, n 比特的格雷码,可以递归地从 n ? 1 比特的格雷码生成。如图 §2-5所示。

//方法 1,利用数学公式,时间复杂度 O(2^n) ,空间复杂度 O(1)
class Solution {
public:
    vector<int> grayCode(int n) {
        int size = pow(2, n); // (1 << n)
        vector<int> result;
        result.reserve(size); //只预留空间,与resize不同
        for(int i = 0; i < size; i++)
        {
            result.push_back(graycodeUtil(i));
        }
        return result;
    }

    // 数学公式,整数 n 的格雷码是 n ⊕ ( n/2)
    int graycodeUtil(int num)
    {
        return num ^ (num / 2); //(num >> 1)
    }
};
时间: 2024-08-12 08:47:18

leetcode_89_Gray Code的相关文章

从微信官方获取微信公众号名片:http://open.weixin.qq.com/qr/code/?username=haihongruanjian

从微信官方获取微信公众号名片:http://open.weixin.qq.com/qr/code/?username=haihongruanjian 个人的号,不知道怎么获取.

微信 {&quot;errcode&quot;:40029,&quot;errmsg&quot;:&quot;invalid code, hints: [ req_id: Cf.y.a0389s108 ]&quot;}

{"errcode":40029,"errmsg":"invalid code, hints: [ req_id: Cf.y.a0389s108 ]"} 问题:微信网页授权后,获取到 openid 了,一刷新又没了 微信网页授权获取到的 code 只能使用一次(5分钟内有效),使用一次后,马上失效. 页面授权跳转成功,根据 code 也换取到 openid 了. 此时刷新页面,并不会再次进行授权,而是直接刷新了一下上一次授权跳转后的链接,带的还是

在linux系统中安装VSCode(Visual Studio Code)

1.从官网下载压缩包(话说下载下来解压就直接可以运行了咧,都不需要make) 访问Visual Studio Code官网 https://code.visualstudio.com/docs?dv=linux64 我是64位的: wget https://az764295.vo.msecnd.net/stable/7ba55c5860b152d999dda59393ca3ebeb1b5c85f/code-stable-code_1.7.2-1479766213_amd64.tar.gz 2.解

set up trace code tool

這以 GNU GLOBAL 6.5.6 為示範 1: install GNU GLOBAL https://www.gnu.org/software/global/download.html sudo ./configure; 若有以下 error,請看更下方的 Q5 說明. configure: checking "location of ncurses.h file"... configure: error: curses library is required but not f

Ubuntu16.04下安装VS Code

在Ubuntu下面安装Visual Studio Code sudo add-apt-repository ppa:ubuntu-desktop/ubuntu-make sudo apt-get update sudo apt-get install ubuntu-make umake web visual-studio-code 终于又看见亲切的大麻花了

CSDN code使用教程之git用法详解

首先需要下载GIT客户端,http://git-scm.com/downloads...   然后再code.csdn.net上面创建一个项目,如果 你的项目已经存在,那么请建立项目的时候不要选择自动生成readme文件.填写项目名称,去掉下面的勾勾,然后点击创建就OK了. 下面的就是配置本地客户端了,确认你在CSDN id,获取的方式是在登录后,进入passport.csdn.net,在"个人帐号"的最下端查看用户名:也就是你的昵称,我的就是Linux_Google 然后在命令行中输

.NET跨平台之mac 下vs code 多层架构编程

合肥程序员群:49313181.    合肥实名程序员群:128131462 (不愿透露姓名和信息者勿加入,申请备注填写姓名+技术+工作年限) Q  Q:408365330     E-Mail:[email protected] 概述: 为了研究跨平台.NET 开发,我打算利用.NET core 编写一个跨平台的cms,这个CMS我也秉着开源的原则放到github上面,为.NET 开源社区做点小小的贡献吧.如果有兴趣的可以联系我一起为.NET开源和跨平台做点小小的贡献吧.EgojitCMS传送

安装GO语言环境之安装Visual Studio Code插件

在安装Visual Studio Code插件的时候,由于谷歌的限制,在下载下列插件的时候会报错: go get -u -v github.com/nsf/gocode go get -u -v github.com/rogpeppe/godef go get -u -v github.com/golang/lint/golint go get -u -v github.com/lukehoban/go-find-references go get -u -v github.com/lukeho

Code First添加一个现有数据库中的表

描述 刚刚使用EF,还没搞明白,遇到下面问题,记录一下. 都说EF好用,一直也没用过,以前写代码都是ADO.NET,写起来费时费力还没什么大进展,如果能把这些事简化一下把精力放到逻辑或者更有用的地方岂不是更好.所以想使用EF.Code First,从字面的意思来看是先有代码后有数据库,通过Model来创建数据库,好像只能是通过Model来生成数据库,至少我接触2天以来是这样,项目已经开始一段时间了,数据库已经有一定的数据,虽然是测试数据,但也不想删掉,从新添加数据也是很烦人的事.想找到一种能够不