题目:

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.

链接: http://leetcode.com/problems/gray-code/

题解:

求灰度值。利用定义来做就可以了。比如 00, 01, 11, 10的下一个灰度值为  000, 001, 011, 010 与   prefix 1再逆序的值 110, 111, 101, 100的和, 就是  000, 001, 010, 011, 110, 111, 101, 100。

public class Solution {
public List<Integer> grayCode(int n) {
List<Integer> res = new ArrayList<Integer>();
if(n < 0)
return res;
if(n == 0) {
res.add(0);
return res;
} List<Integer> last = grayCode(n - 1);
res.addAll(last);
int prefix = (1 << n - 1); for(int i = last.size() - 1; i >= 0; i--)
res.add(last.get(i) + prefix); return res;
}
}

二刷:

Java:

Time Complexity - O(2n), Space Complexity - O(2n)

public class Solution {
public List<Integer> grayCode(int n) {
List<Integer> res = new ArrayList<>();
if (n < 0) {
return res;
}
if (n == 0) {
res.add(0);
return res;
}
List<Integer> last = grayCode(n - 1);
res.addAll(last);
int prefix = 1 << (n - 1);
for (int i = last.size() - 1; i >= 0; i--) {
res.add(prefix + last.get(i));
}
return res;
}
}

也可以用类似memorization来降低space complexity = O(1),不考虑结果集的话

public class Solution {
public List<Integer> grayCode(int n) {
List<Integer> res = new ArrayList<>();
res.add(0);
for (int i = 0; i < n; i++) {
int prefix = 1 << i;
for (int j = res.size() - 1; j >= 0; j--) {
res.add(prefix + res.get(j));
}
}
return res;
}
}

三刷:

方法和上面一样。

Java:

Recursive:

Time Complexity - O(2n), Space Complexity - O(2n)

public class Solution {
public List<Integer> grayCode(int n) {
List<Integer> res = new ArrayList<>();
if (n <= 0) return Collections.singletonList(0);
List<Integer> last = grayCode(n - 1);
res.addAll(last);
int prefix = 1 << (n - 1);
for (int i = last.size() - 1; i >= 0; i--) res.add(prefix + last.get(i));
return res;
}
}

Iterative:

public class Solution {
public List<Integer> grayCode(int n) {
List<Integer> res = new ArrayList<>();
res.add(0);
for (int i = 0; i < n; i++) {
for (int j = res.size() - 1; j >= 0; j--) {
res.add((1 << i) + res.get(j));
}
}
return res;
}
}

Reference:

https://leetcode.com/discuss/2978/what-solution-gray-code-problem-extra-space-used-recursion

https://leetcode.com/discuss/24634/an-accepted-three-line-solution-in-java

https://en.wikipedia.org/wiki/Gray_code

89. Gray Code的更多相关文章

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

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

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

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

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

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

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

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

  6. LeetCode OJ 89. Gray Code

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

  7. 89. Gray Code (Bit)

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

  8. 89. Gray Code(公式题)

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

  9. 【LeetCode】89. Gray Code (2 solutions)

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

随机推荐

  1. python自学笔记二

    :#进入循环重输文0件名 pass else:#退出循环,等待创建 break fobj = open(fname,'a')#打开或创建文件 #接下来写入文件 all = [] print('ente ...

  2. Linux挂载60T存储

    操作系统: CentOS 6.3 存储:总大小为72T,并划分成3个块,每块20T 安装多实例MySQL数据库,不想挂载3个块,弄成一个大的比较方便管理,个人比较懒. 配置多路径:http://blo ...

  3. wap_supplicant介绍

    目前可以使用wireless-tools 或wpa_supplicant工具来配置无线网络.请记住重要的一点是,对无线网络的配置是全局性的,而非针对具体的接口. wpa_supplicant是一个较好 ...

  4. CPU 时间片 分时 轮转调度

    时间片即CPU分配给各个程序的时间,每个线程被分配一个时间段,称作它的时间片,即该进程允许运行的时间,使各个程序从表面上看是同时进行的.如果在时间片结束时进程还在运行,则CPU将被剥夺并分配给另一个进 ...

  5. mysql语句大全

    转自:http://www.cnblogs.com/yunf/archive/2011/04/12/2013448.html   1.说明:创建数据库 CREATE DATABASE database ...

  6. 分析 "End" "Unload Me" "Exit Sub" 之间的区别与联系

    之前就想过这个问题,这么熟悉的几个东西居然对他们分析的不是很透彻. “End”  跟  “Unload  Me”  在敲程序 的时候经常敲到,“exit  sub”  更是熟悉,下面,解析: End  ...

  7. nginx demo

    server_names_hash_bucket_size 512;upstream node_app { server 127.0.0.1:3000; } server { listen 80; s ...

  8. Surrounded Regions

    Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...

  9. 《Dive into Python》Chapter 4 笔记

    自省:Python中万物皆对象,自省是指代码可以查看内存中以对象形式存在的其它模块和函数,获取它们的信息,并对它们进行操作.用这种方法,可以定义没有名称的函数,不按函数声明的参数顺序调用函数,甚至引用 ...

  10. 新建标准mavenWeb工程以及Maven的web应用标准目录结构建议

    到现在为止,使用Maven结构的Web工程越来越多,因此在此介绍一下通过Maven来构建项目的相关知识.     文档主要分为两部分:       1.如何通过maven来构建多模块的web项目    ...