题目:

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. linux c 分解质因数

    #include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> ...

  2. MinGW-64 安装

    一.在mingw-w64官网下载mingw-w64在线安装包 二.点击mingw-w64进行安装,选择: Version:选最新版本 我这个是4.9.2 Architecture:x86_64 (64 ...

  3. R语言基础(二) 可视化基础

    > which.max(apply(x[c("x1","x2","x3")], 1, sum))49 > x$num[which ...

  4. 关于拓扑排序(topologicalsort)

    假设我们有一组任务要完成,并且有些任务要在其它任务完成之后才能开始,所以我们必须非常小心这些任务的执行顺序.如果这些任务的执行顺序足够简单的话,我们可以用链表来存储它们,这是一个很好的方案,让我们可以 ...

  5. win7下Chrome有两个图标的解决方法

    摘抄自:http://www.sevenforums.com/browsers-mail/238406-windows-7-taskbar-creating-double-google-chrome- ...

  6. div 布局2

    转:http://www.kwstu.com/ArticleView/divcss_2013929173533658 关于DIV+CSS布局中用到的CSS必备知识请看:http://www.kwstu ...

  7. silverlight 控件自定义样式 实现方法

    1:在app.xaml中加入需实现的样式,如: <Application.Resources> <Style x:Key="NodeStyle" TargetTy ...

  8. c++ 从标注输入流读取行

    #include <string.h> #include <iostream> #include <vector> #include <stdio.h> ...

  9. 设置DIV隐藏与显示,表格滑动条

    问题描述:         现在希望使用JS设置DIV块的显示与隐藏,当某一个事件触发是,自动显示DIV块,显示表格数据,但是要求表格显示滑动条 问题解决:   (1)DIV块的隐藏与显示 如上所示, ...

  10. action间传多个参数时注意问题

    通常我们action之间传参可以有多种形式,举例说明:示例1: <result name="test" type="redirect-action"> ...