题目:格雷码。

格雷码是从0开始且之后两个相邻码之间只有一个符号不相同,例如000,100,101,111三个相邻之间只有一个二进制不同。

现在给定一个数字n,然后给出格雷码所对应的数字。例如:

For example, given n = 2, return [0,1,3,2]. Its gray code sequence is:

00 - 0

01 - 1

11 - 3

10 - 2

左边一列是格雷码,右边一列是对应的需要输出的十进制。

我们来观察一下格雷码的规律。如果是1那么是

0

1

如果是2的时候,其实就是在0和1前面加0,再在1,0前面加1。

3的时候就是把2的格雷码先加零,然后倒序前面加1,那么我们就可以按照这个规律

000

001

011

010

110

111

101

100

知道怎么来的,而且如果记录的话就是从0开始,往后记,而且因为前面加零的大小是不变的,那么先加入一个0后,之后每次记录在前面加1的即可。那么可以如下:

class Solution {
public:
vector<int> grayCode(int n)
{
vector<int> ans;
ans.push_back(); for (int i = ; i < n; ++i)
{
int left = <<i;
int len = ans.size();
for (int j = len-; j >= ; --j)
ans.push_back(left + ans[j]);
}
return ans;
}
};

leetcode[88] Gray Code的更多相关文章

  1. [LeetCode] 89. 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. 【题解】【排列组合】【回溯】【Leetcode】Gray Code

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

  5. Java for LeetCode 089 Gray Code

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

  6. leetCode 89.Gray Code (格雷码) 解题思路和方法

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

  7. LeetCode题目:Gray Code

    原题地址:https://leetcode.com/problems/gray-code/ class Solution { public: vector<int> grayCode(in ...

  8. Leetcode#89 Gray Code

    原题地址 二进制码 -> 格雷码:从最右边起,依次与左边相邻位异或,最左边一位不变.例如: 二进制: 1 0 0 1 1 1 0 |\|\|\|\|\|\| 格雷码: 1 1 0 1 0 0 1 ...

  9. [LeetCode] Gray Code 格雷码

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

随机推荐

  1. oracle connect by 说明

    Oracle能够通过START WITH . . . CONNECT BY . . .子句来实现SQL分层查询,这递归查询 例如: select level||'月' 月份 from dual con ...

  2. HDU 3366 Passage (概率DP)

    Passage Problem Description Bill is a millionaire. But unfortunately he was trapped in a castle. The ...

  3. Web 服务器 (IIS) 角色

    原文:Web 服务器 (IIS) 角色 1. 对于默认安装,请在命令行提示符下键入以下命令,然后按 Enter: start /w pkgmgr /iu:IIS-WebServerRole;WAS-W ...

  4. Node.js可以做些什么?

    就像 JavaScript 至client天生,Node.js 生于网络.Node.js 我们可以做更多的不是开发一个网络 站这么简单,采用 Node.js.您可以轻松地开发:  具有复杂逻辑的站点 ...

  5. Base64中文不能加密问题

    最近用到了Base64.js来对url参数进行加密,字母和数字都可以很好地加密/解密. 但测试中文时发现不能进行转换,貌似Base64.js不支持中文字符. 联想到encodeURI()对url的编码 ...

  6. 快速解读GC日志(转)

    本文是 Plumbr 发行的 Java垃圾收集手册 的部分内容.文中将介绍GC日志的输出格式, 以及如何解读GC日志, 从中提取有用的信息.我们通过 -XX:+UseSerialGC 选项,指定JVM ...

  7. HDU1253 胜利大逃亡 BFS

    胜利大逃亡 Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submiss ...

  8. acd - 1216 - Beautiful People(DLIS)

    意甲冠军:一个人有两个属性S, B(1 ≤ Si, Bi ≤ 10^9),当两个人满足这两个属性 S1 < S2 && B1 < B2 要么 S1 > S2 & ...

  9. 读取上传的CSV为DataTable

    csv导入文件会把每列的数据用英文逗号分割开来,如果遇到某列中包含英文逗号,则会把该列用英文双引号进行包装. 如果csv文件中某列的数据本身包含英文逗号,应该使用读取字符串的方式进行解析数据,如csv ...

  10. linux_设置开机自启动程序脚本

    设置开机自启动