【Gray Code】cpp
题目:
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.
代码:
class Solution {
public:
vector<int> grayCode(int n) {
std::vector<int> result;
const size_t size = << n;
result.reserve(size);
for (size_t i = ; i < size; ++i)
{
result.push_back(i ^ ( i >> ));
}
return result;
}
};
Tips:
1. gray code与binary code之间的转换公式 “i 异或 (i右移1位)”
2. 这个代码鲁棒性比较强,即使n非常大(比如100000000)也只会返回0,不会crash
3. 另外调研了一下size_t的用法,后面再深入看看。
==========================================
第二次过Gray Code大概能想起来思路,但是记不牢了。看了一下之前的笔记,这道题考察的是位运算。
class Solution {
public:
vector<int> grayCode(int n) {
vector<int> ret;
const size_t N = << n;
ret.reserve(N);
for ( int i=; i<N; ++i ) ret.push_back(i ^ (i >> ));
return ret;
}
};
这里的核心是算2的N次幂可以用1左移N位来计算;反之,除以2,或许也可以用右移1位来操作。
【Gray Code】cpp的更多相关文章
- 【Leet Code】Palindrome Number
Palindrome Number Total Accepted: 19369 Total Submissions: 66673My Submissions Determine whether an ...
- 【转】【VS Code】配置文件Launch及快捷键
Ctrl+shift+p,然后输入launch,点击第一个选项即可配置. 之后选择More即可 具体配置可修改为: { "version": "0.2.0", ...
- hdu 4739【位运算】.cpp
题意: 给出n个地雷所在位置,正好能够组成正方形的地雷就可以拿走..为了简化题目,只考虑平行于横轴的正方形.. 问最多可以拿走多少个正方形.. 思路: 先找出可以组成正方形的地雷组合cnt个.. 然后 ...
- Hdu 4734 【数位DP】.cpp
题意: 我们定义十进制数x的权值为f(x) = a(n)*2^(n-1)+a(n-1)*2(n-2)+...a(2)*2+a(1)*1,a(i)表示十进制数x中第i位的数字. 题目给出a,b,求出0~ ...
- 【WildCard Matching】cpp
题目: Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single charact ...
- 【Implement strStr() 】cpp
题目: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ne ...
- POJ 1018 【枚举+剪枝】.cpp
题意: 给出n个工厂的产品参数带宽b和价格p,在这n个工厂里分别选1件产品共n件,使B/P最小,其中B表示n件产品中最小的b值,P表示n件产品p值的和. 输入 iCase n 表示iCase个样例n个 ...
- Hdu 3410 【单调队列】.cpp
题意: 给出一个数组,问你对于第i个数,从最后一个比它大的数到它之间比它小的数中最大的那个数的下标,以及它右边到第一个比它大的数中比它小的数中最大的那一个数的下标<下标从1开始>. eg: ...
- Hdu 4539 【状态DP】.cpp
题意: 一个炮兵可以攻打和他之间曼哈顿距离为2的士兵,给出你一块n*m的战场,告诉你哪些地方可以站人哪些地方不可以,问你最多可以安放多少个士兵? n <= 100, m <= 10 思路: ...
随机推荐
- uLua学习之读取外部Lua脚本(四)
前言 上节说到了Lua脚本与unity3d中C#脚本的数据交互,但是我感觉上节中的数理方式不太好,因为我们是把Lua脚本以字符串形式粘贴到C#脚本中的,如果读取配置数据都这样做的话,那就太可怕了.想想 ...
- node18 服务器上 pytorch cyclegan 测试有问题,numpy 版本不对
提示如下错误: module compiled against API version 0xb but this version of numpy is 0xa 尝试的方法: pip install ...
- IFEO 映像文件劫持
“映像劫持”,也被称为“IFEO”(Image File Execution Options) 映像劫持的根本就是被恶意篡改了注册表HKEY_LOCAL_MACHINE\SOFTWARE\Micros ...
- SqlServer触发器的理解
SqlServer触发器是与表事件相关的特殊存储过程,它的执行不是由程序调用,也不是手工启动,而是由事件来触发.比如当对一个表进行操作( insert,delete, update)时就会激活它执行. ...
- Apache 错误日志
配置文件中:httpd.conf ErrorLog "/usr/local/var/log/apache2/error_log1" CustomLog "/usr/loc ...
- Ubuntu18 中文乱码 问题 解决
之前租的服务器没有中文乱码的问题,最近重装了一下系统, 出现了中文乱码, 以下是解决方案: 输入locale查看当前的语言是否是中文 root@ubuntu:~# locale LANG=zh_CN. ...
- GNU汇编程序框架
汇编的作用:1.对芯片进行初始化 2. 和C混合编程提升C的运行效率 .section .data < 初始化的数据> .section .bss <未初始化的数据> .sec ...
- node操作mogondb数据库的封装
注:摘自网络 上面的注释都挺详细的,我使用到了nodejs的插件mongoose,用mongoose操作mongodb其实蛮方便的. 关于mongoose的安装就是 npm install -g mo ...
- Ubuntu12.04下YouCompleteMe安装教程(部分)
1.通过源码编译安装VIM 开发中使用的是Ubuntu 12.04 LTS,通过sudo apt-get install vim安装的版本较低,不支持YCM,所以,用源码编译并安装最新的Vim. 卸载 ...
- OAuth2.0 social_django微博第三方登录
python网站第三方登录,social-auth-app-django模块, social-auth-app-django模块是专门用于Django的第三方登录OAuth2协议模块 目前流行的第三方 ...