gray-code——找规律
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——找规律的更多相关文章
- URAL 1780 G - Gray Code 找规律
G - Gray CodeTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view ...
- POJ 1850 Code(找规律)
Code Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 7913 Accepted: 3709 Description ...
- LeetCode89:Gray Code
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- [LeetCode] Gray Code 格雷码
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- 【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 (middle)
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- BZOJ-1228 E&D 博弈SG+找啊找啊找规律
讨厌博弈,找规律找半天还是错的.... 1228: [SDOI2009]E&D Time Limit: 10 Sec Memory Limit: 162 MB Submit: 666 Solv ...
- leetcode[88] Gray Code
题目:格雷码. 格雷码是从0开始且之后两个相邻码之间只有一个符号不相同,例如000,100,101,111三个相邻之间只有一个二进制不同. 现在给定一个数字n,然后给出格雷码所对应的数字.例如: Fo ...
- 【一天一道LeetCode】#89. Gray Code
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 The gra ...
- (DP 雷格码)Gray code -- hdu -- 5375
http://acm.hdu.edu.cn/showproblem.php?pid=5375 Gray code Time Limit: 2000/1000 MS (Java/Others) M ...
随机推荐
- linux系统web日志分析脚本
linux系统web日志分析这方面工具比较多,比如logwatch或awstats等使用perl语言开发,功能都非常强大.但这些软件都需要进行一些配置,很多朋友往往在技术方面没有投入太多力量,即便参照 ...
- ubuntu 下openoffice安装
openoffice官网建议的安装步骤:http://www.openoffice.org/download/index.html 前提条件 如果你希望Java集成,你要确保你有安装最新的JRE.它的 ...
- 【bzoj2096】[Poi2010]Pilots 双指针法+STL-set
题目描述 Tz又耍畸形了!!他要当飞行员,他拿到了一个飞行员测试难度序列,他设定了一个难度差的最大值,在序列中他想找到一个最长的子串,任意两个难度差不会超过他设定的最大值.耍畸形一个人是不行的,于是他 ...
- hihoCoder #1758 加减
$\DeclareMathOperator{\lowbit}{lowbit}$ 题目大意 对于一个数 $x$,设它最低位的 1 是第 $i$ 位,则 $\lowbit(x)=2i$ . 例如 $\lo ...
- BZOJ3462 DZY Loves Math II 【多重背包 + 组合数】
题目 输入格式 第一行,两个正整数 S 和 q,q 表示询问数量. 接下来 q 行,每行一个正整数 n. 输出格式 输出共 q 行,分别为每个询问的答案. 输入样例 30 3 9 29 1000000 ...
- linux系统初始化——启动脚本是如何工作的
启动脚本是如何工作的 Linux 使用的是基于 运行级(run-levels) 概念的称为 SysVinit 的专用启动工具.它在不同的系统上可能是完全不一样的,所以不能认为一个脚本在某个 Linux ...
- Windows系统——后缀为.zip.00X的zip分卷解压
Windows下后缀为*.zip.001文件的解压方法: 后缀为*.zip.001文件用winrar无法解压, 解决办法是在windows下打开命令行界面, 输入:copy /B xx.zip.001 ...
- 使用 swagger组件给asp.net webapi文档生成
1.名词解释 Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 Web 服务.总体目标是使客户端和文件系统作为服务器以同样的速度来更新.文件的方法,参数和模 ...
- 收集邮票(bzoj 1426)
Description 有n种不同的邮票,皮皮想收集所有种类的邮票.唯一的收集方法是到同学凡凡那里购买,每次只能买一张,并且买到的邮票究竟是n种邮票中的哪一种是等概率的,概率均为1/n.但是由于凡凡也 ...
- 【POJ1067】取石子游戏(威佐夫博弈)
题意:有两堆石子,数量任意,可以不同.游戏开始由两个人轮流取石子. 游戏规定,每次有两种不同的取法,一是可以在任意的一堆中取走任意多的石子:二是可以在两堆中同时取走相同数量的石子. 最后把石子全部取完 ...