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. redis宕机时哨兵的处理

    https://blog.csdn.net/a67474506/article/details/50435498 redis宕机是的故障处理 重启故障机 sentinel.conf 的配置会改变

  2. Cortex-M3 异常中断响应与返回

    [异常/中断响应]Cortex-M3的异常/中断响应序列包括: 入栈:把8个寄存器的值压入栈. 取向量:从向量表中找出对应的服务程序入口地址. 更新寄存器:更新堆栈指针SP,更新连接寄存器LR,更新程 ...

  3. 操作系统-Windows:UWP(Universal Windows Platform)

    ylbtech-操作系统-Windows:UWP(Universal Windows Platform) 1.返回顶部 1. UWP即Windows 10中的Universal Windows Pla ...

  4. user_tab_columns和user_col_comments区别

    SELECT USER_TAB_COLUMNS.COLUMN_NAME, USER_COL_COMMENTS.COMMENTS, CASE WHEN INSTR(USER_TAB_COLUMNS.DA ...

  5. asp.net core mvc View Component 应用

    ViewComponent 1.View 组件介绍 在ASP.NET CORE MVC中,View组件有点类似于partial views,但是他们更强大,View组件不能使用model bindin ...

  6. linux下解决80端口被占用

    安装一个nginx服务,在启动的时候报80端口被占用了,我们来检查一下有哪些服务占用了80端口 首先我们查一下占用80端口的有哪些服务,netstat -lnp|grep 80 查看80端口被那些服务 ...

  7. eclipse 安装TestNg

    通过eclipse安装TestNg,过程如下: 1.点击help-->Install New Software 2.打开如下窗口,点击add,name自定义输入,Location中输入http: ...

  8. Pycharm从虚拟环境切换到本地环境

    切换到本地: 点击左上角File – settings , 在打开的对话框中选择:Project xxx (xxx是你项目的名称) – Project Interpreter ,在右边可以看到解释器, ...

  9. 【ABAP系列】SAP ABAP如何在调试查看EXPORT/IMPORT 内存数据

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP如何在调试查看E ...

  10. Linux常用命令详解(1)

    基础命令: ls man pwd cd mkdir echo touch cp mv rm rmdir cat more less head tail clear poweroff reboot 命令 ...