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.

Example 1:

Input: 2
Output: [0,1,3,2]
Explanation:
00 - 0
01 - 1
11 - 3
10 - 2 For a given n, a gray code sequence may not be uniquely defined.
For example, [0,2,3,1] is also a valid gray code sequence. 00 - 0
10 - 2
11 - 3
01 - 1

Example 2:

Input: 0
Output: [0]
Explanation: We define the gray code sequence to begin with 0.
  A gray code sequence of n has size = 2n, which for n = 0 the size is 20 = 1.
  Therefore, for n = 0 the gray code sequence is [0].

规律:

1. 如果格雷码n位数,那么返回的数组长度是2^n(即1 << n)

2. 二进制-->格雷码:如果二进制码字的第i位和i+1位相同,则对应的格雷码的第i位为0,否则为1。最高为之前可以看作还有一位0,即最高位是和0异或。

*格雷码-->二进制:最左边一位不变,从左边第二位起,将每位与左边一位解码后的值异或,作为该位解码后的值。

class Solution {
public List<Integer> grayCode(int n) {
int length = 1 << n;
List<Integer> ret = new ArrayList<Integer>();
ret.add(0);
int i = 1;
int num;
while(i < length){
num = i^(i>>1); //异或,相同为0,不同为1
ret.add(num);
i++;
}
return ret;
}
}

89. Gray Code (Java)的更多相关文章

  1. 89. Gray Code - LeetCode

    Question 89. Gray Code Solution 思路: n = 0 0 n = 1 0 1 n = 2 00 01 10 11 n = 3 000 001 010 011 100 10 ...

  2. 89. Gray Code

    题目: The gray code is a binary numeral system where two successive values differ in only one bit. Giv ...

  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. 【LeetCode】89. Gray Code

    题目: The gray code is a binary numeral system where two successive values differ in only one bit. Giv ...

  5. 89. Gray Code(中等,了解啥是 gray code)

    知道啥是 gray code 就是收获了. 下面介绍了 gray code 发明的 motivation, 了解动机后就知道啥是 gray code 了. https://zh.wikipedia.o ...

  6. 【一天一道LeetCode】#89. Gray Code

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 The gra ...

  7. 89. Gray Code返回位运算的所有生成值

    [抄题]: The gray code is a binary numeral system where two successive values differ in only one bit. G ...

  8. LeetCode OJ 89. Gray Code

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

  9. 89. Gray Code (Bit)

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

随机推荐

  1. 再谈用Excel计算年龄

    有的时候,对于客人的信息并不是全知,那么身份证就可能用15位来代替,这个时候怎么计算年龄呢?有一个很简单的公式,可以一次性计算15位或18位身份证的年龄. 首先,需要判断一下,这个身份证是15位还是1 ...

  2. [SQL Server 视图的创建- (create view必须是批处理中仅有的语句问题)]

    当我们SQL Server在创建视图时 ,会出现"create view 必须是批处理中仅有的语句"这个语法错误提示 ,实际上这本身没什么错! 因为create view 必须是批 ...

  3. python 类中__call__内置函数的使用

    class F: def __call__(self, *args, **kwargs): print('执行__call__') s = F()s() 先给类创建一个对象,直接通过对象来执行,就会自 ...

  4. clientX和clientY属性需要注意的地方

    clientX和clientY为可视区鼠标的位置. 1. 随鼠标移动的div块[runjs] 当document有多个页面时,会出现问题.[runjs] 2. 解决方案:scrollTop, scro ...

  5. Python:Base2(List和Tuple类型, 条件判断和循环,Dict和Set类型)

    1.Python创建list: Python内置的一种数据类型是列表:list.list是一种有序的集合,可以随时添加和删除其中的元素. 比如,列出班里所有同学的名字,就可以用一个list表示: &g ...

  6. 记一次flannel调试

    今天发现k8s集群中不同Node上的pod不能互相ping通.调试了很长时间,发现是flannel的问题,记之. 问题一:对flannel backend的选择 flannel支持多种backend, ...

  7. python-Web-数据库-Redis

    概述: >>>安装: >>>数据类型: string(字符串),hash(哈希),list(列表),set(集合)及zset(sorted set:有序集合) &g ...

  8. uni-app编译配置

    Uni-app 编译配置 <!-- #ifdef H5 --> <view>只在H5编译</view> <!-- #endif --> <!-- ...

  9. 小程序入门 MQTT物联网协议 publish 和订阅subscribe onenet 阿里云 百度云 基于GPRS模块(SIM800C/SIM900A/SIM868等)和STM32主控芯片

    本文基本公开了如何移植MQTT物联网协议到STM32平台上,并结合GPRS模块(SIM800C/SIM900A/SIM868等)实现publish和订阅topic从onenet,阿里云,百度云等.如果 ...

  10. Smarty快速入门

    1.Smarty是用纯php语言写的类 2.功能是实现前后端分离 3.Smarty简洁高效 4.快速入门案例 1.下载 smarty源码 https://www.smarty.net/ 2.搭建PHP ...