Gray Code
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 - 2Note:
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.
以上是题目。想的时候要先把问题减少到最小规模,也就是1位。然后再扩展成两位,再扩展成3位,从而发现当位数增加时低位的变化规律。把问题简化到最小规模,且不改变问题本质的方法非常适用于解决实际问题。
这题的数字变化规律是: 每向高位增加一位,则所有低位的二进制按照从下到上的逆序复制一遍就行了。比如例子中的3,看成是最高位的1和3上一个数字对应3的低位部分的复制(复制的是1的位)。4就是最高位的1加上复制的0的位。写成代码如下:
vector<int> grayCode(int n) {
vector<int> res;
res.push_back(0);
if (n > 0)
res.push_back(1); int cnt = 2;
for (int k = 1; k < n; k++)
{
int preBitsNum = std::pow(2, k);
/*if (preBitsNum == 1)
preBitsNum = 0;*/
for (int i = 0; i < preBitsNum; i++)
{
int cur = 1 << k;
cur |= res[cnt++ - i * 2 - 1];
res.push_back(cur);
}
}
return res;
}
Gray Code的更多相关文章
- [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 ...
- [LintCode] Gray Code 格雷码
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- 44. Decode Ways && Gray Code
Decode Ways A message containing letters from A-Z is being encoded to numbers using the following ma ...
- LeetCode——Gray Code
Description: The gray code is a binary numeral system where two successive values differ in only one ...
- LeetCode:Gray Code(格雷码)
题目链接 The gray code is a binary numeral system where two successive values differ in only one bit. Gi ...
- [LeetCode]题解(python):089 Gray Code
题目来源 https://leetcode.com/problems/gray-code/ The gray code is a binary numeral system where two suc ...
- 【题解】【排列组合】【回溯】【Leetcode】Gray Code
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
随机推荐
- XtraBackup原理解读
XtraBackup是现今为止唯一一款为InnoDB 和XtraDB提供热备的开源工具,这个工具有以下的有点: (1)备份快速高效而且可靠 (2)备份过程可以做到事物处理不间断 (3)节省磁盘空间和网 ...
- Badboy使用数据源Excel进行脚本参数化
1.首先新建一个Excel,这里示例我写得非常简单,由两由数据组成,第一行为表头.见下图: 2.录制脚本,见上一篇,录制一个非常简单的搜狗查询 3.添加数据源,在Tools面板中找到Data Sour ...
- new和alloc的区别
简单来说,new和alloc在功能上基本没有什么区别,都是分配内存,初始化对象. 但是,调用new的话,只能通过默认的init方法来初始化对象,而alloc可以通过其他的初始化方法如:-(instan ...
- 关于git中git pull --rebase中的一些坑
在公司里面,每次我更改完代码,准备pull最新代码时,总是会遇到各种各样的问题.,因为对应的问题不同,解决方法很多.但是比较通用的办法还是有的: git pull --rebase //报错时 g ...
- 【项目】搜索广告CTR预估(二)
项目介绍 给定查询和用户信息后预测广告点击率 搜索广告是近年来互联网的主流营收来源之一.在搜索广告背后,一个关键技术就是点击率预测-----pCTR(predict the click-through ...
- lucene和ElasticSearch基本概念
lucene基本概念 索引(Index) 对应一个倒排表,一个检索的基本单位.在lucene中就对应一个目录. lucene基本概念 段(Segment) 一个索引可以包含多个段,段与段之间是独立的, ...
- python之 list、tuple、dict、set
2016-08-24 14:32:59 list Python内置的一种数据类型是列表:list.list是一种有序的集合,可以随时添加和删除其中的元素. 例如:定义一个列表L,里面存放了一些姓氏 ...
- js中的call与apply深入浅出
首先明确call()与apply()最大的区别,除了名字不同以外,就是参数不一样,call的参数需要一一列出,apply的第二个及其以后的参数需要组成一个数组传进来. 这两个函数的调用者不是对象,而是 ...
- Codeforces Round #381 (Div. 2) 复习倍增//
刷了这套题 感触良多 我想 感觉上的差一点就是差很多吧 . 每次都差一点 就是差很多了... 不能气馁..要更加努力去填补那一点点. 老天不是在造物弄人,而是希望你用更好的自己去迎接自己. A. ...
- ubuntu14.04 安装系统
p { margin-bottom: 0.1in; line-height: 120% } code.cjk { font-family: "Droid Sans Fallback" ...