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=2,它的结果包含n=1时的结果左边补零,以及逆序遍历n=1时的结果左边补1。

规律例如以下图,列出了n=1,n=2,n=3时的情况。

依据这个规律假设已知n=k的情况,那么n=k+1的结果包含对n=k的结果左边补零,即保存不变。然后逆序遍历n=k的结果左边补1就可以。

runtime:4ms

class Solution {
public: vector<int> grayCode(int n) {
vector<int> result(1);
for(int i=0;i<n;i++)
{
for(int j=result.size()-1;j>=0;j--)
{
result.push_back((1<<i)+result[j]);
}
}
return result;
}
}。

解法二

解法二就涉及到gray code的数学知识了。要是知道这个数学知识。能够在几分钟之内就解出这道题。

格雷码能够由相应的十进制数求出:grayCode=i^i>>1

runtime:4ms

class Solution {
public:
vector<int> grayCode(int n) {
vector<int> result;
for(int i=0;i<1<<n;i++)
{
result.push_back(i^i>>1);
}
return result;
} };

LeetCode89:Gray Code的更多相关文章

  1. Leetcode89. Gray Code格雷编码

    给定一个代表编码总位数的非负整数 n,打印其格雷编码序列.格雷编码序列必须以 0 开头. 示例 1: 输入: 2 输出: [0,1,3,2] 解释: 00 - 0 01 - 1 11 - 3 10 - ...

  2. [Swift]LeetCode89. 格雷编码 | Gray Code

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

  3. [LeetCode] Gray Code 格雷码

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

  4. 【LeetCode】Gray Code

    Gray Code The gray code is a binary numeral system where two successive values differ in only one bi ...

  5. Gray Code

    Gray Code The gray code is a binary numeral system where two successive values differ in only one bi ...

  6. 【leetcode】Gray Code (middle)

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

  7. [LintCode] Gray Code 格雷码

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

  8. 44. Decode Ways && Gray Code

    Decode Ways A message containing letters from A-Z is being encoded to numbers using the following ma ...

  9. LeetCode——Gray Code

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

随机推荐

  1. ButterKnifeZelezny简单使用教程

    https://github.com/avast/android-butterknife-zelezny     一,配置butterknife Configure your project-leve ...

  2. 字典对象的 Pythonic 用法(上篇)

    字典对象在Python中作为最常用的数据结构之一,和数字.字符串.列表.元组并列为5大基本数据结构,字典中的元素通过键来存取,而非像列表一样通过偏移存取.笔者总结了字典的一些常用Pyhonic用法,这 ...

  3. python redis-string、list、set操作

    string操作 redis中的string在内存中都是按照一个key对应一个value来存储的 方法: set() 方法 : 写入一条数据 mset() 方法: 写入多条数据 , 可是Key-Val ...

  4. group by的运用

    select a.* from zeai_photo a inner join (select max(id) mid,userid from zeai_photo group by userid) ...

  5. php 高并发解决方案(用于抢购) 转载

    最近在做一个团购项目,遇到个问题,就是在抢购.秒杀.抽奖等活动时,库存数量有限,但是同时下单人数超过了库存数量,就会导致商品超售问题.那么我们怎么来解决这个问题呢,我的思路如下: sql1:查询商品库 ...

  6. 域名 DNS命令——dig

    dig命令详解   1.查看域名的A记录          # dig yahoo.com; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.10.rc1.e ...

  7. hbase学习(一)hbase简介

    1.hadoop生态系统 2.hbase简介 非关系型数据库知识面扩展 cassandra.hbase.mongodb.redis couchdb,文件存储数据库 Neo4j非关系型图数据库 3.hb ...

  8. poj 1041(字典序输出欧拉回路)

    John's trip Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8641   Accepted: 2893   Spe ...

  9. [BZOJ3150][Ctsc2013]猴子 期望dp+高斯消元

    3150: [Ctsc2013]猴子 Time Limit: 20 Sec  Memory Limit: 256 MBSec  Special JudgeSubmit: 163  Solved: 10 ...

  10. bzoj 1853 容斥 + 搜索

    思路:先把所有幸运数字找出来, 把没有用的去掉,然后爆搜容斥,因为最多只会搜十几个就超过限制了, 所以是可行的. #include<bits/stdc++.h> #define LL lo ...