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. Android res之shape

    xml控件配置属性 android:background="@drawable/shape" 标签 corners ----------圆角gradient ----------渐 ...

  2. python jieba分词小说与词频统计

    1.知识点 """ 1)cut() a) codecs.open() 解决编码问题 b) f.readline() 读取一行,也可以使用f.readlines()读取多行 ...

  3. [redis]redis实现分页的方法

    每个主题下的用户的评论组装好写入Redis中,每个主题会有一个topicId,每一条评论会和topicId关联起来,大致的数据模型如下:{ topicId: 'xxxxxxxx', comments: ...

  4. Xcode8.1 真机测试 ,添加iOS10.3的idk到Xcode8.1中

    1.下载iOS10.3的idk包; 2.解压, 找到路径 Finder -> 应用程序 -> 右键Xcode -> 显示包内容 -> Contents -> Develo ...

  5. 【HANA系列】SAP HANA SQL从给定日期中获取月份

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP HANA SQL从给定日 ...

  6. 【Deep Learning Nanodegree Foundation笔记】第 10 课:Sentiment Analysis with Andrew Trask

    In this lesson, Andrew Trask, the author of Grokking Deep Learning, will walk you through using neur ...

  7. NOIp2015D1T3 斗地主【暴搜】

    题目传送门 刚开始读到题目的时候,非常懵逼,非常崩溃,写着写着呢,也有点崩溃,细节有点多. 这个做法呢,只能过掉官方数据,洛谷上好像有加强版,只能得$86$分,就没有管了. 大概说一下思路: 暴力搜索 ...

  8. flutter环境配置(windows)

    参考以下链接 下载安装步骤: https://www.cnblogs.com/yangyxd/p/8809512.html 安装Flutter环境遇到Android license status un ...

  9. 影响mysql主从延迟速度的相关参数

    1.sync-binlog MySQL提供一个sync_binlog参数来控制数据库的binlog刷到磁盘上去. 默认,sync_binlog=0,表示MySQL不控制binlog的刷新,由文件系统自 ...

  10. SQL注入之手工注入

    手工注入 用的是墨者学院的靶场:传送门 涉及以下数据库: MySQL.Access.SqlServer(MSSQL).SQLite.MongoDB.Db2(IBM).PostgreSQL.Sybase ...