【leetcode】Gray Code (middle)
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)的更多相关文章
- 【LeetCode】Gray Code
Gray Code The gray code is a binary numeral system where two successive values differ in only one bi ...
- 【题解】【排列组合】【回溯】【Leetcode】Gray Code
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- 【Leetcode】【Medium】Gray Code
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- 【leetcode】Sort List (middle)
Sort a linked list in O(n log n) time using constant space complexity. 思路: 用归并排序.设输入链表为S,则先将其拆分为前半部分 ...
- 【leetcode】Subsets II (middle) ☆
Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...
- 【leetcode】Word Search (middle)
今天开始,回溯法强化阶段. Given a 2D board and a word, find if the word exists in the grid. The word can be cons ...
- 【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 ...
- 【leetcode】 Palindrome Partitioniong (middle) (*^__^*)
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- 【leetcode】 Generate Parentheses (middle)☆
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
随机推荐
- MEAN组合框架搭建教程
1,我们先走在官方github里面下载个包文件: git clone https://github.com/linnovate/mean.git (是慢了点) 2,我把这个文件解压后文件名叫mean ...
- Markdown 写作工具选择
Markdown 写作工具选择 候选产品 参考了少数派网站 markdown 写作工具2015年度盘点 http://sspai.com/32483, Windows 下 Markdown 的编辑工具 ...
- iOS 滑动性能优化
iOS 滑动性能优化 目录 一. 减少图层的Blend操作 1. UIView的背景色避免使用clearColor 2. 控件贴图避免使用带alpha的图片 3. UIImageView 使用时避免半 ...
- PHP基础之 错误处理 及 异常处理
错误处理: 1.使用die()方法,结束语句的执行,并输出错误消息 2.自定义错误和错误触发器 自定义错误处理函数(系统有默认的错误处理函数,自定义的错误处理会覆盖默认的处理函数) ========= ...
- 【PHP面向对象(OOP)编程入门教程】7.特殊的引用”$this“的使用
现在我们知道了如何访问对象中的成员,是通过”对象->成员”的方式访问的,这是在对象的外部去访问对象中成员的形式, 那么如果我想在对象的内部,让对象里的方法访问本对象的属性, 或是对象中的方法去调 ...
- springmvc之前后台传值
一.向后台传值 1.项目结构 2.jar包 3.spring-config.xml <?xml version="1.0" encoding="UTF-8" ...
- c语言小知识点
大一时学c语言,总结的一些自己感觉很零碎且容易忘的知识点,不对之处请指正 1.字符串不管中间是否有数值0,结尾一定有数值02.浮点类型的变量存储并不精确3.printf格式串自动右对齐,加负号左对齐4 ...
- weapp微信小程序初探demo
https://github.com/donglegend/weapp-demo 参考文档开发工具安装微信weapp API git项目源码微信小程序 demo效果展示效果预览
- BZOJ2229—— [Zjoi2011]最小割
0.题目大意:求两点之间的最小割,然后找出其中小于x的数量 1.分析:最小割树水题,上个板子就好 #include <queue> #include <ctime> #incl ...
- gradle类重复的问题解决方法
今天遇到一个gradle的类重复问题,学习到一个命令 gradle -q dependencies,可以查看项目里包的依赖关系,发生这个错误是因为我用了一个相册的项目,这个项目里用到了v4包,我自己的 ...