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. 黑客攻防技术宝典Web实战篇(三)web攻击方式总结

    web攻击的手段无非就是使服务器资源耗尽,使服务器无法接收正常请求. 一.DDos攻击 二.DRDos攻击 三.慢攻击 与Ddos攻击相反,慢攻击并不是以多取胜,而是靠保持连接.

  2. Android与Dalvik

    自学android的同事说:Android 这个妈蛋!! 当初就应该选择c++/c 来开发.现在为了效率又搞ART.简直是折腾,art在android5.0 的时候就是默认了.前段时间还在学习andr ...

  3. 如何一次把所有wordpress插件都禁用了

    wordpress网站出现了问题,或者在更新一些数据的时候,需要先把插件全部都禁用了进行检查.那么如何一次把所有wordpress插件都禁用呢?试试下面的sql语句吧 UPDATE wp_option ...

  4. Mac OS 中的 Python(和 NumPy)开发环境设置

    http://www.python()tab.com/html/2013/pythonjichu_1010/582.html ()需要删除

  5. http之错误码

    http://en.wikipedia.org/wiki/List_of_HTTP_status_codes 响应码由三位十进制数字组成,它们出现在由HTTP服务器发送的响应的第一行.响应码分五种类型 ...

  6. 百度浏览器+hao123评价

    1.用户界面: 界面比较简洁,没有多余的没用的东西.在界面上部有天气,比较方便用户查看天气,中间有各个实用性网站和大家通常使用较多的网站,可以比较快的查看.侧栏有些比较有针对性的内容.上部还有可以静音 ...

  7. 关于CSS的优先级,CSS优先级计算

    原则一: 继承不如指定原则二: #id > .class > 标签选择符原则三:越具体越强大原则四:标签#id >#id ; 标签.class > .class CSS优先级权 ...

  8. CentOS 6.5安装Apache

    1.Apache的特点 功能强大.配置简单.速度快.应用广泛.性能稳定可靠,并可做代理服务器或负载均衡来使用 2.Apache的应用场合 使用Apache运行静态HTML网页.图片(处理静态小文件能力 ...

  9. Android 如何在Eclipse中查看Android API源码 及 support包源码

    当我们阅读android API开发文档时候,上面的每个类,以及类的各个方法都是已经写好的方法和控件,可是我们只是在搬来使用,不知道它的原理,它是如何被实现的.android系统是开源的,所以谷歌官方 ...

  10. HDU 3999 二叉排序树

    The order of a Tree Problem Description The shape of a binary search tree is greatly related to the ...