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 ...
随机推荐
- ASP.NET中的母版页机制
项目中用到了母版页,由于好长时间没用了,不太熟悉起原理,在网上找了一下: http://www.cnblogs.com/_zjl/archive/2011/06/12/2078992.html 有时间 ...
- python学习第四天第一部分
1.字典的特性:无序.去重.查询速度快.比list占用内存多. 2.字典查询速度快的原因:因为他是哈希类型的. 3.什么是(hash)哈希? hash把任意长度的二进制映射为较短的固定长度的二进制,这 ...
- android----sqlite中的 query() 参数分析
public Cursor query (String table, String[] columns, String selection, String[] selectionArgs, Strin ...
- java转义字符
JAVA中转义字符: 1.八进制转义序列:\ + 1到3位5数字:范围'\000'~'\377' \0:空字符 2.Unicode转义字符:\u + 四个十六进制数字:0~65535 ...
- npm,grunt,less,sass,typescript
typescript http://www.typescriptlang.org sass http://sass-lang.com/ less http://lesscss.org/ bow ...
- EditorWindow 和MenuItem
using UnityEngine; using System.Collections; using UnityEditor; public class ClipEventEditor : Edito ...
- Unity Camera属性
Camera属性 1.Clear Flags 清除标记:决定屏幕的那部分将被清除.当使用多个相机来描绘不同的游戏景象时,利用它是非常方便的. 2.Background 背景:在镜头中的所有元素描绘完成 ...
- oracle里如何将两个日期的时间差返回**时**分的格式
SELECT EXTRACT(DAY FROM (sysdate-to_date('2012-03-29 00:00:00','YYYY-MM-DD HH24:MI:ss')) DAY TO SECO ...
- Visual Studio Code asp.net 5环境搭建技能Get
启动准备阶段 预热 1 VS Code 官方地址:https://www.visualstudio.com/en-us/products/code-vs.aspx 2 安装Node.js :https ...
- Codeforces Round #302 (Div. 2) D. Destroying Roads 最短路
题目链接: 题目 D. Destroying Roads time limit per test 2 seconds memory limit per test 256 megabytes input ...