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. Tomcat的SSL证书配置以及Tomcat+Nginx实现SSL配置

    把jks上传到java容器在的服务器上,路径只要不是webapps下就可以,然后到conf目录下server.xml里配置 <Connector port=" protocol=&qu ...

  2. Python语法一

    前记,今天开始学习Python 参考 笨方法学习+Python(第三版) 因为有编程基础,所以入门不难,相比于以前学过的其它语言编程,Python当然也有它独特的语法格式. 1.安装Python 访问 ...

  3. Diamond使用向导

    一.背景描述                     Diamond是淘宝内部使用的一个管理持久配置的系统,它的特点是简单.可靠.易用,目前淘宝内部绝大多数系统的配置,由diamond来进行统一管理. ...

  4. CheckedComboBoxEdit 重置初始化值的方法

    CheckedComboBoxEdit ccbgb; ccbgb绑定方式ccbgb .Properties .DataSource =数据集; ccbgb.Properties.DisplayMemb ...

  5. 15.6.2 Configuring the Merge Threshold for index pages[innodb]

    MERGE THRESHOLD 提供了可以合并相邻索引page的功能. 默认值是50 如果一个页中数据被删除或者更新减小,导致页中有空白部分,空白部分接近合并门槛的值,则会和相邻页合并, 但是两个pa ...

  6. VFP调用SOAPTOOLKIT 低级API

    Connector=CREATEOBJECT("mssoap.HttpConnector")Connector.Property["EndPointURL"] ...

  7. 团队编程——web应用之人事管理系统

    本次作业为团队作业,团队博客要求如下:1. 介绍团队情况:包括队长.成员.队名.成员照片.队训--.等:2. 介绍团队项目名称.总体任务,各成员任务等:3. 每个队做 一次需求调研(针对团队项目),要 ...

  8. Regex

    1. regex with variable example: find the number and put a parenthese around the number. output: a(52 ...

  9. java序列化深拷贝

    java深拷贝 序列化和反序列化合成在一起的方法CloneUtils import java.io.ByteArrayInputStream; import java.io.ByteArrayOutp ...

  10. C++的一些小的知识点

    1.初始化: 对于内置类型 ]; // 10个未初始化int ](); // 10个值初始化为0的int 对于自定义类型: 只要一调用new,那么编译器不仅仅给它分配内存,还调用它的默认构造函数初始化 ...