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. embed 层级太高

    它怎么就好了,凭什么就好了.为什么就好了.我到底当时是哪里写错了.怎么个情况 兼容Firefox ,IE的flash透明和flash置底代码 <object classid="clsi ...

  2. SqlServer代理执行[分发清除: distribution] 无法删除快照文件

    每天偶尔检查数据库作业是否失败,发现有错误 select top 10 job_id,run_date,run_time,run_duration,step_name,message from  ms ...

  3. qstring与char*、基本数据类型的转换

    1.qstring转化为char* QString.toStdString.c_str() 2.char*转化为QString str = QString(QLatin1String(mm)); 3. ...

  4. rpc优化

    1.刷文章列表的时候,发现调用总时间100ms ,其中调策略是花了60ms,一个开源的map方法dozer,组装bean要花40ms 2.redis的zounct方法,传 1和-1的时候有时候会返回0 ...

  5. windows下调用外部exe程序 SHELLEXECUTEINFO

    本文主要介绍两种在windows下调用外部exe程序的方法: 1.使用SHELLEXECUTEINFO 和 ShellExecuteEx SHELLEXECUTEINFO 结构体的定义如下: type ...

  6. Redis学习笔记九:独立功能之慢查询日志

    Redis 的慢查询日志用于记录执行时间超过给定时长的命令请求,用户可以通过这个功能产生的日志来监视和优化查询速度. 服务器配置有两个相关选项: slowlog-log-slower-than 选项指 ...

  7. Github如何更新远程代码

    1.git add . 2.git commit -m "Second commit" 3.git push origin master

  8. (转)listview中常见难题总结

    原文地址:http://blog.csdn.net/cherry609195946/article/details/8844224 1. PopWindow中listview的item获取不到点击事件 ...

  9. tomcat管理端的页面安全措施

    由于公司的项目并未启用nginx负载均衡,所以自然也没用到tomcat与web应用一对一的安全操作,经常会遇到 重启单个应用又不想重启tomcat的情况.同时,又出于安全考虑,将tomcat的默认管理 ...

  10. mysql日期加减<转>

    1. MySQL 为日期增加一个时间间隔:date_add() set @dt = now(); select date_add(@dt, interval 1 day); - 加1天 select ...