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

思路: Gray Code的意思就是n位的二进制序列,相邻两个只有一位不同。观察

000 - 0
001 - 1
011 - 3
010 - 2
110 - 6
111 - 7
101 - 5
100 - 4

第0位的序列为 01 10 01 这样不断重复

第1位的序列为 0011 1100

第2位的序列为 11110000

这样我们就找到了规律。

我最开始是通过判段每次是第几位变化,通过异或得到新值。 每次第k为变化时满足 i = 2k + n*2(k+1)

vector<int> grayCode(int n) {
vector<int> ans;
ans.push_back();
if(n == )
return ans; for(int i = ; i < ( << n); i++)
{
int bit_change = ;
for(int j = ; j < n; j++)
{
if(i % ( << (j + )) - ( << j) == )
{
bit_change = j; break;
}
}
int cur = ( << bit_change);
cur ^= ans.back();
ans.push_back(cur);
}
return ans;
}

后来看其他人的发现更简单的方法

vector<int> grayCode2(int n) {
vector<int> ans;
ans.push_back();
if(n == )
return ans; for(int i = ; i < n; i++)
{
int inc = << i;
for(int j = ans.size() - ; j >= ; j--) //每次等第i - 1位正反序都存完毕时,第i位起始为0的情况也存储完了, 只需存储第i位起始为1并且低位倒序输出
{
ans.push_back(ans[j] + inc);
}
}
return ans;
}

【leetcode】Gray Code (middle)的更多相关文章

  1. 【LeetCode】Gray Code

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

  2. 【题解】【排列组合】【回溯】【Leetcode】Gray Code

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

  3. 【Leetcode】【Medium】Gray Code

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

  4. 【leetcode】Sort List (middle)

    Sort a linked list in O(n log n) time using constant space complexity. 思路: 用归并排序.设输入链表为S,则先将其拆分为前半部分 ...

  5. 【leetcode】Subsets II (middle) ☆

    Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...

  6. 【leetcode】Word Search (middle)

    今天开始,回溯法强化阶段. Given a 2D board and a word, find if the word exists in the grid. The word can be cons ...

  7. 【leetcode】 Permutation Sequence (middle)

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  8. 【leetcode】 Palindrome Partitioniong (middle) (*^__^*)

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

  9. 【leetcode】 Generate Parentheses (middle)☆

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

随机推荐

  1. [设计模式] javascript 之 策略模式

    策略模式说明 定义: 封装一系列的算法,使得他们之间可以相互替换,本模式使用算法独立于使用它的客户的变化. 说明:策略模式,是一种组织算法的模式,核心不在于算法,而在于组织一系列的算法,并且如何去使用 ...

  2. Ionic 常见问题及解决方案

    前言 Ionic是目前较为流行的Hybird App解决方案,在Ionic开发过程中会遇到很多常见的开发问题,本文尝试对这些问题给出解决方案. 一些常识与技巧 list 有延迟,可以在ion-cont ...

  3. mysql搜索引擎 小结

    mysql搜索引擎 小结 mysql5.5以后,mysql默认使用InnoDB存储引擎. 若要修改默认引擎,可以修改配置文件中的default-storage-engine.可以通过show vari ...

  4. nyoj 4 779 兰州烧饼

    兰州烧饼 时间限制:1000 ms  |  内存限制:65535 KB 难度:1   描述 烧饼有两面,要做好一个兰州烧饼,要两面都弄热.当然,一次只能弄一个的话,效率就太低了.有这么一个大平底锅,一 ...

  5. Ubuntu使用ApkTool进行APK反编译

    1.Apktool下载 http://ibotpeaches.github.io/Apktool/ 下载最新版本Apktool_2.1.1.jar 2.新建一个apktool目录,将Apktool_2 ...

  6. eclipse emacs

    eclipse emacs 插件 http://www.mulgasoft.com/emacsplus eclipse字体设置: 一.把字体设置为Courier New  操作步骤:打开Elcipse ...

  7. 京东云、新浪微博等专家畅谈Docker未来格局:开放与竞争(下)

    在上次推送的文章中(传送门),田琪老师分享了他的DockerCon 2015峰会见闻.在“QCon高可用架构群”中,田老师分享之后,几位专家也参与了讨论.他们是: 闫国旗:京东资深架构师,京东架构技术 ...

  8. IE hack

    .hack{ color:#fff;background:#; background:#06f\; /* all IE */ background:#\; /* IE8-9 */ background ...

  9. Maven的依赖范围

    Maven的依赖构件包含一个依赖范围属性,这个属性描述的是三套classpath的控制,即编译.测试.运行. 举个例子Junit依赖只是在测试范围(classpath)使用,而在运行的时候不使用,还有 ...

  10. Linus爱GPL 但不喜欢GPL诉讼

    导读 在上周多伦多举行的LinuxCon NA会议上,Linus Torvalds 和 VMware 副总裁 Dirk Hohndel讨论了 GNU GPL 在 Linux 成功上所起的作用.Hohn ...