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=0    0

n=1    0    0+1<<(1-1)

n=2    n=1: 0  1   1+1<<(2-1)   0+1<<(2-1)

找到规律,第n次增加的序列为n-1序列的倒序+(高位+1)形成。

 class Solution {
public:
vector<int> grayCode(int n) {
vector<int> res;
if(n<) return res;
res.push_back();
for(int i=;i<=n;i++){
int size=res.size();
for(int j=size-;j>=;j--){
int tmp=<<(i-);
tmp|=res[j];
res.push_back(tmp);
}
} return res;
}
};

gray-code——找规律的更多相关文章

  1. URAL 1780 G - Gray Code 找规律

    G - Gray CodeTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view ...

  2. POJ 1850 Code(找规律)

    Code Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 7913   Accepted: 3709 Description ...

  3. LeetCode89: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 格雷码

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

  5. 【LeetCode】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. BZOJ-1228 E&D 博弈SG+找啊找啊找规律

    讨厌博弈,找规律找半天还是错的.... 1228: [SDOI2009]E&D Time Limit: 10 Sec Memory Limit: 162 MB Submit: 666 Solv ...

  8. leetcode[88] Gray Code

    题目:格雷码. 格雷码是从0开始且之后两个相邻码之间只有一个符号不相同,例如000,100,101,111三个相邻之间只有一个二进制不同. 现在给定一个数字n,然后给出格雷码所对应的数字.例如: Fo ...

  9. 【一天一道LeetCode】#89. Gray Code

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 The gra ...

  10. (DP 雷格码)Gray code -- hdu -- 5375

    http://acm.hdu.edu.cn/showproblem.php?pid=5375 Gray code Time Limit: 2000/1000 MS (Java/Others)    M ...

随机推荐

  1. c语言有用函数收集

    1 strtok :分解字符串为一组字符串.s为要分解的字符串,delim为分隔符字符串.首次调用时,s指向要分解的字符串,之后再次调用要把s设成NULL. 2 strstr :从字符串str1中查找 ...

  2. Android获取系统时间yyyyMMddHHmmssSSS

    代码改变世界 public String testTime1() throws ParseException { String DEFAULT_TIME_FORMAT = "yyyy-MM- ...

  3. Win右键管理员权限的获取

    Windows Registry Editor Version 5.00 ;取得文件修改权限 [HKEY_CLASSES_ROOT\*\shell\runas] @="管理员权限" ...

  4. 文件处理.Windows.Fastcopy.3.50.x64.文件复制简体中文破解版(验证版)

    摘要:Fastcopy 3.50 + x64 绿色汉化中文版由知索网汉化发布.Fastcopy 是一款来自日本的最快的文件拷贝工具.磁盘间相互拷贝文件是司空见惯的事情,通常情况都是利用 WINDOWS ...

  5. 刷题总结—— Scout YYF I(poj3744 矩阵快速幂+概率dp)

    题目: Description YYF is a couragous scout. Now he is on a dangerous mission which is to penetrate int ...

  6. UVA 1619/POJ2796 滑窗算法/维护一个单调栈

    Feel Good Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 12409   Accepted: 3484 Case T ...

  7. Intellij热部署插件JRebel

    Intellij热部署插件JRebel 安装JRebel 激活JRebel 相关设置 Intellij热部署插件JRebel 项目需求,一直用eclipse的我,也要改用IDEA了,一开始,很不习惯. ...

  8. poj 3250 Bad Hair Day

    Bad Hair Day Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 21084   Accepted: 7202 Des ...

  9. HDU 4487 Maximum Random Walk

    Maximum Random Walk Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  10. js判断手机的左右滑动

    js代码 $(function() { function judge() { var startx;//让startx在touch事件函数里是全局性变量. var endx; var el = doc ...