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.

解题思路:

格雷码,学过通信原理的童鞋应该都不陌生,只不过格雷码的生成方法不是按照题目中描述的那样,下面科普下格雷码的生成方法:

n=1 得到0 1

n=2 得到00 01 11 10(注意,11 和 10除了符号位外,和 00 01对称的)

n=3 得到000 001 011 010 110 111 101 100(后面四位除了符号位之外,和前四位对称)

n=4 依次类推

知道了格雷码的生成过程,那么JAVA实现如下:

   static public List<Integer> grayCode(int n) {
List<Integer> list=new ArrayList<Integer>();
for(int i=0;i<Math.pow(2, n);i++){
int result=0,num=i,temp=n;
while(temp>1){
if(num>=Math.pow(2, temp-1)){
num=(int) (2*Math.pow(2, temp-1)-num-1);
result+=Math.pow(2, temp-1);
}
temp--;
}
result+=num;
list.add(result);
}
return list;
}

同时,本题有一个非常简洁的写法,如下:

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

Java for LeetCode 089 Gray Code的更多相关文章

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

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

  2. [LeetCode] 89. Gray Code 格雷码

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

  3. 【LeetCode】Gray Code

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

  4. 【leetcode】Gray Code (middle)

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

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

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

  6. leetcode[88] Gray Code

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

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

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

  8. 089 Gray Code 格雷编码

    格雷编码是一个二进制数字系统,在该系统中,两个连续的数值仅有一个位数的差异.给定一个代表编码总位数的非负整数 n,打印格雷码序列.格雷码序列必须以0开头.例如, 给定 n = 2, 返回 [0,1,3 ...

  9. LeetCode题目:Gray Code

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

随机推荐

  1. Web.config配置文件详解(新手必看) 【转】

    来源 :http://www.cnblogs.com/gaoweipeng/archive/2009/05/17/1458762.html 花了点时间整理了一下ASP.NET Web.config配置 ...

  2. tp框架where条件查询数据库

    tp框架where条件查询数据库 Where 条件表达式格式为: $map['字段名'] = array('表达式', '操作条件'); 其中 $map 是一个普通的数组变量,可以根据自己需求而命名. ...

  3. MySQL具体解释(14)----------事务处理

    前言:前一篇文章关于事务处理的博文没有写清楚,读起来非常晦涩.非常难理解,所以有整理了一些资料,帮助理解.见谅! 关于MySQL事务处理学习记 START TRANSACTION COMMIT ROL ...

  4. dom控制

    (1)创建新节点 createDocumentFragment()    //创建一个DOM片段 createElement_x_x()   //创建一个具体的元素 createTextNode()  ...

  5. Oracle 字段类型

    Oracle 字段类型 http://www.cnblogs.com/lihan/archive/2010/01/06/1640547.html 字段类型 描述 字段长度及其缺省值 CHAR (siz ...

  6. 获取input光标的x和y轴

    http://blog.csdn.net/kingwolfofsky/article/details/6586029 index.html <!DOCTYPE html> <html ...

  7. JavaScript实现对象数组按不同字段排序

    如果有一个对象数组,我们想要依据某个对象属性对数组进行排序.而传递给数组sort()方法的比較函数要接收两个參数,即要比較的值.但是.我们须要一种方式来指明依照哪个属性来排序.要解决问题,能够定义一个 ...

  8. python sax解析xml

    #books.xml<catalog> <book isbn="0-596-00128-2"> <title>Python & XML& ...

  9. openwrt patch

    一: 这几天使用一款电信的4G网卡,发现了一些问题,所以决定打个pitch来解决问题,顺便把patch的生成与使用学习一下 二:安装patch的管理工具quilt 1. sudo apt-get in ...

  10. VIM中保存编辑的只读文件

    如何在VIM中保存编辑的只读文件 你是否会和我一样经常碰到这样的情景:在VIM中编辑了一个系统配置文件,当需要保存时才发现当前的用户对该文件没有写入的权限.如果已 经做了很多修改,放弃保存的确很懊恼, ...