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 ...
随机推荐
- spring加载properties配置文件
public static void main(String[] args){ String path="E:/workspace/bocMarketData/src/config/P ...
- C# 使用Sqlite 如何返回统计行数
Visual 2010 with Sqlite 需要这样Query 数据: select count(*) from tblOrder where OrderStartTime >= '2013 ...
- maven install 报错Could not calculate build plan: Plugin org.apache.maven.plugins:maven-resources-plugin
Could not calculate build plan: Plugin org.apache.maven.plugins:maven-resources-plugin:2.6 or one of ...
- 在Servlet中使用spring注入的bean
package abu.csdn.servlet; import java.io.IOException; import javax.servlet.ServletContext; ...
- linux性能优化常用命令
作为一名linux系统管理员,最主要的工作是优化系统配置,使应用在系统上以最优的状态运行,但是由于硬件问题.软件问题.网络环境等的复杂性 和多变性,导致对系统的优化变得异常复杂,如何定位性能问题出在哪 ...
- Linux目录和权限
1. rmdir -p 用来删除一串目录,是否可以成功删除? rmdir -p 删除一个不存在的目录时是否报错呢?rmdir -p 不能成功删除非空目录,rmdir -p 删除一个不存在的目录 ...
- AngularJS初步
AngularJS特点 遵循AMD规范 不需要操作节点 对于jquery,一般是利用现有完整的DOM,然后在这戏Dom的基础上进行二次调教了:而对于AngularJS等框架则是根据数据模型以及其对应用 ...
- 黑马程序员—— Java SE(3)
----<a href="http://www.itheima.com" target="blank">Java培训.Android培训.iOS培训 ...
- javascript将毫秒还原为可读时间格式
<script type="text/javascript"> //随便设置一个时间 var otime = new Date("2015-11-11 20: ...
- sql 2000 分页
create PROCEDURE [dbo].[Proc_GetPageList] ( @Tables varchar(1000), --表名 @PK varchar(100 ...