题目:

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. HotSpot Builder Utility安装指南

    系统需求硬件- 一台带有1个以太网卡的电脑(宿主机)- 一个无线路由器 软件- VirtualBox 4.1或更高的版本.下载网址:http://www.virtualbox.org/- 我们提供的最 ...

  2. wap_supplicant介绍

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

  3. edge.js架起node.js和.net互操作桥梁

    今天要介绍的是edge.js这个github上刚兴起的开源项目,它可以让node.js和.net之间在in-process下互操作..net版本在4.5及以上,因为.net4.5带来的Task,asy ...

  4. 说说iOS中的手势及触摸

    一.响应链 在IOS开发中会遇到各种操作事件,通过程序可以对这些事件做出响应. 首先,当发生事件响应时,必须知道由谁来响应事件.在IOS中,由响应者链来对事件进行响应,所有事件响应的类都是UIResp ...

  5. PHP中日期时间函数date()用法总结

    date()是我们常用的一个日期时间函数,下面我来总结一下关于date()函数的各种形式的用法,有需要学习的朋友可参考. 格式化日期date() 函数的第一个参数规定了如何格式化日期/时间.它使用字母 ...

  6. 【学习总结】声明变量在@interface括号中与使用@property的区别

    方式一:直接在.h文件@interface中的大括号中声明. @interface Test : NSObject { NSString *str; // 私有变量 , 其他类无法访问,只能够该类内部 ...

  7. 简单制作mib表

    今天放假后第一天上班,将假前自学制作mib表的东西说一下. 在这里呢,我以世界-中国-上海-闵行这种包含关系介绍,感觉更容易理解. MIB file的开始和结束 所有的MIB file的都以DEFIN ...

  8. MarkdownPad2添加目录(输出为HTML时可用)

    平时看书的时候懒得上网写在线博客,就在电脑上用了很长时间的MarkDownPad2来记录自己的心得笔记,等那天高兴了再把他们贴出来.界面清爽,是我使用它最重要的原因,但是MarkdownPad2导出的 ...

  9. c++ std::string 用法

    std::string用法总结 在平常工作中经常用到了string类,本人记忆了不好用到了的时候经常要去查询.在网上摘抄一下总结一下,为以后的查询方便: string类的构造函数: string(co ...

  10. 使用SpringMVC+mybatis+事务控制+JSON 配置最简单WEB

    最近在总结一些项目的基础知识,根据公司最近的一些意向和技术路线,初步整理了一个简单的配置例子     1.使用springmvc代替strutsMVC     2.使用请求json数据串的方式代替传统 ...