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. iOS开发之plist文件操作

    之前在想用代码去实现很多界面上能用interface builder可以拖进去的功能,现在想想真是够无知的啊.不仅效率低下,而且对于代码的维护带来不少麻烦,这段时间一直在反思看了design+code ...

  2. 对于JVM中方法区,永久代,元空间以及字符串常量池的迁移和string.intern方法

    在Java虚拟机(以下简称JVM)中,类包含其对应的元数据,比如类的层级信息,方法数据和方法信息(如字节码,栈和变量大小),运行时常量池,已确定的符号引用和虚方法表. 在过去(当自定义类加载器使用不普 ...

  3. 获取Android设备的方向,Sensor和SensorManager实现手机旋转角度

    http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2012/1009/425.html 带有g-sensor的Android设备上可通过API ...

  4. Poi读取Excle报错 java.util.zip.ZipException: invalid stored block lengths

    一:Poi读取Excle报错  java.util.zip.ZipException: invalid stored block lengths 系统中需要导出excle签收单,excle模板是预设好 ...

  5. 20145312 《Java程序设计》第2周学习总结

    20145312 <Java程序设计>第2周学习总结 教材学习内容总结 1.1类型.变量与运算符 1.1.1类型 1.基本类型 整数:short(2字节).long(8字节).int(4字 ...

  6. linux及安全第三周总结——20135227黄晓妍

    总结部分: Linux内核源代码: Arch 支持不同cpu的源代码:主要关注x86 Init   内核启动的相关代码:主要关注main.c,整个Linux内核启动代码start_kernel函数 K ...

  7. Finder Quick Menu FAQ

    How to use Finder Quick Menur: 1. Start Finder Quick Menu.2. Open "System Preferences -> Ext ...

  8. storm(二) 事务机制

    前言 为了保证tuple的强有序和exactly-once语义,storm提供了事务机制,为每个tuple提供一个id 设计方法1 为每个tuple设置一个事务id,在数据库保存事务id和当前处理的i ...

  9. c# 过滤HTML代码 源代码,案例 下载

    #region 过滤HTML代码 //替换掉html字符,只显示文字信息. public string replaceHtmlCode(string Htmlstring) { Htmlstring ...

  10. share point CSOM 客户端模式 创建表 增删改查

    需要引用:Microsoft.SharePoint.Client ascx: <h4>CSOM所有表名</h4> <table> <tr> <td ...