【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 思路: ...
随机推荐
- Miner3D Enterprise 企业版
——极致的视觉数据挖掘和知识发现解 Miner3D Enterprise 企业版是一个有力的综合数据驱动的三维可视化和数据分析解决方案.金融师.药剂师.生物技术或化学材质科研人员.地质学家.石油开采专 ...
- C#添加删除防火墙例外(程序、端口)
一. 添加 COM 引用 在引用里,选择 COM 页, 找到 NetFwTypeLib , 确定即可 二. 添加允许通过防火墙的例外程序 using System; using System.Coll ...
- 关闭VAX的拼写检查_解决中文红色警告问题
菜单VAssistX->Visual Assistant X Options->Advanced->Underlines下 取消“Underline spelling errors ...
- TestNG并发测试包
https://www.yiibai.com/testng/basic-annotations.html
- git剖析
GIT(分布式版本控制系统) Git是一款免费.开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目. 1.特点 分布式相比于集中式的最大区别在于开发者可以提交到本地,每个开发者通过克隆(g ...
- matplotlib学习之(四)设置线条颜色、形状
本文是学习<matplotlib for python developers>的一点笔记plot画图时可以设定线条参数.包括:颜色.线型.标记风格.1)控制颜色颜色之间的对应关系为b--- ...
- YII2 定义页面提示
控制器里面这样写: 单条消息: 键值是规定好的,不要去自定义哦! \Yii::$app->getSession()->setFlash('error', 'This is the mess ...
- Dll注入:Ring3 层 APC注入
APC,即Asynchronous procedure call,异步程序调用APC注入的原理是:在一个进程中,当一个执行到SleepEx()或者WaitForSingleObjectEx()时,系统 ...
- this.value = this.placeholder || this.getAttribute('placeholder')
this.value = this.placeholder || this.getAttribute('placeholder') 鉴于不同的浏览器对为止属性的实现方式有所不用,这里同时使用了HTML ...
- 3203 数组做函数参数----排序函数--C语言版
3203: 数组做函数参数----排序函数--C语言版 时间限制: 1 Sec 内存限制: 128 MB提交: 253 解决: 151[提交][状态][讨论版][命题人:smallgyy] 题目描 ...