Gray Code 解答
Question
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.
Solution
Key to the solution is to find that we can get grey code of n by grey code of (n - 1).
A simple illustration of from 2 to 3

When input is n, grey code size is 2n.
1. For first 2n - 1, we just get from grey code of (n - 1)
2. For second 2n - 1, we traverse grey code of (n - 1) in back order, and add 1 to head for each element.
public class Solution {
public List<Integer> grayCode(int n) {
if (n == 0) {
List<Integer> result = new ArrayList<Integer>();
result.add(0);
return result;
}
List<Integer> result = grayCode(n - 1);
int length = result.size();
int addNumber = 1 << (n - 1);
for (int i = length - 1; i >= 0; i--) {
int tmp = result.get(i);
result.add(addNumber + tmp);
}
return result;
}
}
Gray Code 解答的更多相关文章
- LeetCode OJ 89. Gray Code
题目 The gray code is a binary numeral system where two successive values differ in only one bit. Give ...
- [LeetCode] Gray Code 格雷码
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- 【LeetCode】Gray Code
Gray Code The gray code is a binary numeral system where two successive values differ in only one bi ...
- Gray Code
Gray Code The gray code is a binary numeral system where two successive values differ in only one bi ...
- 【leetcode】Gray Code (middle)
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- [LintCode] Gray Code 格雷码
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- 44. Decode Ways && Gray Code
Decode Ways A message containing letters from A-Z is being encoded to numbers using the following ma ...
- LeetCode——Gray Code
Description: The gray code is a binary numeral system where two successive values differ in only one ...
- LeetCode:Gray Code(格雷码)
题目链接 The gray code is a binary numeral system where two successive values differ in only one bit. Gi ...
随机推荐
- 哲学家用餐问题的几个解法(c语言实现)
参考资料: 1.维基百科:哲学家用餐问题 2.Windows的多线程编程
- hdu 4512 吉哥系列故事——完美队形I_LCIS
题目链接 题意: 假设有n个人按顺序站在他的面前,他们的身高分别是h[1], h[2] ... h[n],吉哥希望从中挑出一些人,让这些人形成一个新的队形,新的队形若满足以下三点要 求,则就是新 ...
- poj 3176 Cow Bowling(dp基础)
Description The cows don't use actual bowling balls when they go bowling. They each take a number (i ...
- SecureCRT 安装及初始化配置
安装 SecureCRT 7.3.4 安装以及破解方法 SecureCRT 6.5.0 汉化解压版 初始化配置 这里配置以SecureCRT 6.5.0 汉化解压版为例 1.调整SecureCRT终端 ...
- AVR32开发环境搭建
下面是搭建AVR32开发环境的过程记录: 1.AVR32的编译环境下载 (到这里下载 as5installer-stable-5.1.208-full.exe) 如果你在安装的过程中碰到如下问题: ...
- mongodb.open失效导致访问地址404
今天做编辑文章功能的时候发现一个问题,编辑并保存完成后再次跳转到当前文章所在的地址,结果报404,打断点发现查询数据库的时候mongodb.open方法失效.百度后找到了原因: 编辑保存的时候打开了数 ...
- TravelCMS旅游网站系统诞生记
本人就是一纯粹码农,没什么学历,更没什么技术,但是自认为学习能力还不错,近期有一个旅游网站系统项目正在进行中,在此以贴图记录这个项目的诞生过程,本是一个定制系统,但是不想把系统做死,以通用产品的标准来 ...
- (转)CommandArgument用法
1.绑定数据库中一个主键前台代码:<ItemTemplate> <asp:ImageButton ID="ibtnUpdate ...
- Sql Server 查看所有存储过程或视图的位置及内容
and a.object_id = b.object_id and a.[type] in ('P','V','AF') order by a.[name] asc 通过这个sql语句可以查到sql ...
- HashMap陷入死循环的例子
//使用这个例子可以模拟HashMap陷入死循环的效果,可能需要执行多次才会出现. 1 package com.hanzi; import java.util.HashMap; public clas ...