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

看着好像很难的样子,但是实际上就是一个二进制码到格林吗的转换而已,代码如下(这个本科的时候好像学过,但是不记得了,看了下别人的):

 class Solution {
public:
vector<int> grayCode(int n) {
int total = << n;
vector<int> ret;
for(int i = ; i < total; ++i){
ret.push_back(i>>^i);
}
return ret;
}
};

java版本的代码如下所示:

 public class Solution {
public List<Integer> grayCode(int n) {
List<Integer> ret = new ArrayList<Integer>();
int num = 1 << n;
for(int i =0 ; i< num; ++i){
ret.add(i>>1^i);
}
return ret;
}
}

还有一种是总结规律的写法:

可以利用遞歸,在每一層前面加上0或者1,然後就可以列出所有的格雷碼。比如:
第一步:產生 0, 1 兩個字符串。
第二步:在第一步的基礎上,每一個字符串都加上0和1,但是每次只能加一個,所以得做兩次。這样就變成了 00,01,11,10 (注意對稱)。
第三步:在第二步的基礎上,再给每個字符串都加上0和1,同样,每次只能加一個,這样就變成了 000,001,011,010,110,111,101,100。
好了,這样就把3位元格雷碼生成好了。
如果要生成4位元格雷碼,我們只需要在3位元格雷碼上再加一層0,1就可以了: 
0000,0001,0011,0010,0110,0111,0101,0100,1100,1101,1110,1010,0111,1001,1000.

 
也就是說,n位元格雷碼是基於n-1位元格雷碼產生的。
 
如果能夠理解上面的部分,下面部分的代碼實現就很容易理解了。
 public String[] GrayCode(int n) {

         // produce 2^n grade codes
String[] graycode = new String[(int) Math.pow(, n)]; if (n == ) {
graycode[] = "";
graycode[] = "";
return graycode;
} String[] last = GrayCode(n - ); for (int i = ; i < last.length; i++) {
graycode[i] = "" + last[i];
graycode[graycode.length - - i] = "" + last[i];
} return graycode;
}

LeetCode OJ: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 格雷码

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

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

    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. leetcode[88] Gray Code

    题目:格雷码. 格雷码是从0开始且之后两个相邻码之间只有一个符号不相同,例如000,100,101,111三个相邻之间只有一个二进制不同. 现在给定一个数字n,然后给出格雷码所对应的数字.例如: Fo ...

  6. 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 ...

  7. 【LeetCode】Gray Code

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

  8. 【leetcode】Gray Code (middle)

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

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

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

  10. gray code 格雷码 递归

    格雷码 the n-1 bit code, with 0 prepended to each word, followd by the n-1 bit code in reverse order, w ...

随机推荐

  1. maven 介绍(一)

    本文内容主要摘自:http://www.konghao.org/index 内部视频                              http://www.ibm.com/developer ...

  2. SQL.Mysql中Cast()函数的用法

    比起orcale,MySQL相比之下就简单得多了,只需要一个Cast()函数就能搞定.其语法为:Cast(字段名 as 转换的类型 ),其中类型可以为: CHAR[(N)] 字符型  DATE  日期 ...

  3. 企业和开发人员究竟该如何适应web标准?

    以下几点注意事项仅供参考:完善的前期策划和分析完善的前期逻辑模型以及项目规范性文档的制定尽可能将行政性干预移到策划阶段(按照国内的情况,做到这一点可能很困难)尽可能向后兼容,在项目规范性文档制定阶段对 ...

  4. ss+proxifier灵活控制网络代理

    SS相比大家都知道,不多说. proxifier可能知道的不是很多(至少在今天之前我是不知道的...可能我孤陋寡闻吧) 之前用ss基本上就是chrome SwitchyOmega+SS实现chrome ...

  5. Nginx将不同IP的请求分发到不同的WEB服务器

    server { listen ; server_name localhost; large_client_header_buffers 16k; client_max_body_size 300m; ...

  6. 20145240《网络对抗》逆向及Bof基础实践

    逆向及Bof基础实践 1.1 实践目标 本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何用户输入的字符串. 该程序同时包 ...

  7. 记一次redis key丢失的问题排查

    最近测试环境的redis经常性发生某些key丢失的问题,最终的找到的问题让人大吃一惊. 复盘一下步骤: 1.发现问题 不知道从某天开始,后台经常报错,原因是某些key丢失,一开始不在意,以为是小bug ...

  8. [HAOI2015]T2

    [题目描述] 有一棵点数为N的树,以点1为根,且树点有边权.然后有M个操作,分为三种: 操作1:把某个节点x的点权增加a. 操作2:把某个节点x为根的子树中所有点的点权都增加a. 操作3:询问某个节点 ...

  9. SaltStack应用grains和jinja模板-第四篇

    目标需求 1.使用jinja模板让apache配置监听本地ip地址 2.了解grains的基本使用方法 说明:实验环境是在前面的第二篇和第三篇基础上完成 实现步骤 使用grains获取ip地址信息 使 ...

  10. ASP.NET OAuth Authorization - Difference between using ClientId and Secret and Username and Password

      What I don't fully understand is the use of ClientId and Secret vs Username and Password. The code ...