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.

以上是题目。想的时候要先把问题减少到最小规模,也就是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的更多相关文章

  1. [LeetCode] Gray Code 格雷码

    The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...

  2. 【LeetCode】Gray Code

    Gray Code The gray code is a binary numeral system where two successive values differ in only one bi ...

  3. 【leetcode】Gray Code (middle)

    The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...

  4. [LintCode] Gray Code 格雷码

    The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...

  5. 44. Decode Ways && Gray Code

    Decode Ways A message containing letters from A-Z is being encoded to numbers using the following ma ...

  6. LeetCode——Gray Code

    Description: The gray code is a binary numeral system where two successive values differ in only one ...

  7. LeetCode:Gray Code(格雷码)

    题目链接 The gray code is a binary numeral system where two successive values differ in only one bit. Gi ...

  8. [LeetCode]题解(python):089 Gray Code

    题目来源 https://leetcode.com/problems/gray-code/ The gray code is a binary numeral system where two suc ...

  9. 【题解】【排列组合】【回溯】【Leetcode】Gray Code

    The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...

随机推荐

  1. SQL Server 数据库的安全管理(登录、角色、权限)

    ---数据库的安全管理 --登录:SQL Server数据库服务器登录的身份验证模式:1)Windows身份验证.2)Windows和SQL Server混合验证 --角色:分类:1)服务器角色.服务 ...

  2. javascript算法

    代码运行环境: nodejs + mochajs /* *选择排序 *每次查找数组最小数据 *将最小数据排到左侧 */ var assert = require('assert'); describe ...

  3. PDF的信息表达原理及特点分析

    一.PDF概述 PDF(Portable Document Format)是一种结构化的文档格式.它由美国著名排版与图像处理软件Adobe公司于1993年首次发布(1.0版),并于同年推出了其相应的支 ...

  4. 用R实现全排列的分类

    R 其实是个很好用的东东哦-最近写了个小函数,可以实现全排列数的枚举,代码如下: permut<-function(seq){     seq_len=length(seq);     if(s ...

  5. javascript篇-----函数apply()和call()

    转自:http://www.jb51.net/article/28013.htm 如果没接触过动态语言,以编译型语言的思维方式去理解javaScript将会有种神奇而怪异的感觉,因为意识上往往不可能的 ...

  6. [知识整理]Java集合(一) - List

    一.实现List的几个类: ArrayList.LinkedList.CopyOnWriteArrayList.Vector 二.几个List底层的数据结构: ArrayList - 数组列表 Lin ...

  7. Markdown 快速入门

    使用Markdown编辑器:MarkdownPad 2 标题: # 标题 ## 标题 ### 标题 #### 标题 ##### 标题 ###### 标题 效果: 标题 标题 标题 标题 标题 标题 下 ...

  8. ---JS canvas学习笔记

    context的fillStyle属性 fillStyle=color | gradient | image | canvas |video strokeStyle也有上述属性. 1.color:#f ...

  9. MY SQL8.0里程碑发布

    MySQL 开发团队于 12 日宣布 MySQL 8.0.0 开发里程碑版本(DMR)发布! 可能有人会惊奇 MySQL 为何从 5.x 一下跳跃到了 8.0.事实上,MySQL 5.x 系列已经延续 ...

  10. Powershell获取并导出指定日期EventLog

    $date = Get-Date 28/07/16 Get-EventLog -After $date "Application"|Export-Csv c:\ming.csv