89. Gray Code
题目:
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的更多相关文章
- 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 ...
- 【LeetCode】89. Gray Code
题目: The gray code is a binary numeral system where two successive values differ in only one bit. Giv ...
- 89. Gray Code(中等,了解啥是 gray code)
知道啥是 gray code 就是收获了. 下面介绍了 gray code 发明的 motivation, 了解动机后就知道啥是 gray code 了. https://zh.wikipedia.o ...
- 【一天一道LeetCode】#89. Gray Code
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 The gra ...
- 89. Gray Code返回位运算的所有生成值
[抄题]: The gray code is a binary numeral system where two successive values differ in only one bit. G ...
- LeetCode OJ 89. Gray Code
题目 The gray code is a binary numeral system where two successive values differ in only one bit. Give ...
- 89. Gray Code (Bit)
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- 89. Gray Code(公式题)
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- 【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 ...
随机推荐
- 从零开始学ios开发(十二):Table Views(上)
这次学习的控件非常重要且非常强大,是ios应用中使用率非常高的一个控件,可以说几乎每个app都会使用到它,它就是功能异常强大的Table Views.可以打开你的iphone中的phone.Messa ...
- 深入PHP EOF(heredoc)用法详解
介绍下使用EOF heredoc方式,输出长段内容的方法, <?php $name = '姓名'; print <<<EOT <html> <head> ...
- API网关
API网关 最开始只是想找个API网关防止API被恶意请求,找了一圈发现基于Nginx的OpenResty(Lua语言)扩展模块Orange挺好(也找了Kong,但是感觉复杂了点没用),还偷懒用Vag ...
- 实现 iframe 子页面调用父页面中的js方法
父页面:index.html(使用iframe包含子页面child.html) [xhtml] view plaincopyprint? <html> <head> <s ...
- C# 客户端判断服务器连接已断开
问题描述: 在C# Socket编程中,服务器端已经断开连接(发送数据方),客户端接收服务器端发送数据,在客户端使用client.Recieve()中,服务器端断开连接,客户端任然显示已 ...
- trie树(前缀树)
问题描述: Trie树,即字典树,又称单词查找树或键树,是一种树形结构,是一种哈希树的变种.典型应用是用于统计和排序大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计.它的优 ...
- Java多线程——<二>将任务交给线程,线程声明及启动
一.任务和线程 <thinking in java>中专门有一小节中对线程和任务两个概念进行了具体的区分,这也恰好说明任务和线程是有区别的. 正如前文所提到的,任务只是一段代码,一段要达成 ...
- 项目中libevent几个问题
几个问题: .libevent到底用的是select还是iocp,然后是如何突破64限制的 typedef struct fd_set { u_int fd_count; /* how many ar ...
- 找不到对应的webservice配置参数[ProcessService]
在UI端 保存时 界面显示无法保存 且报此错误 “找不到对应的webservice配置参数[ProcessService]” 此下为解决方法: 首先 在[应用管理平台]--[参数模板设置] 找到你的参 ...
- uva 10330 最大流
拆点 将节点 i 的容量拆成从 i 到 i+n 的边的容量 套用最大流模板 ac #include <cstdio> #include <cstdlib> #include ...