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 ...
随机推荐
- hdu1540-Tunnel Warfare (线段树区间合并)
题意:n个村庄,有三种操作,D x 破坏位置为x的村庄,R 修复上一次被破坏的村庄,Q x 输出含有x村庄的连续村庄的最大个数.线段树搞之,区间合并. ls[maxn]为当前节点左面的连续区间,rs[ ...
- 文件夹65ad47d7-2e27-4a5c-b238-26643fdaeb98
这几天发现电脑中毒了,本地开的服务预览页面时,页面会被插入Html代码.我用360扫描之后发现有木马病毒(c:\programData有个65ad47d7-2e27-4a5c-b238-26643fd ...
- Android UI开发详解之ActionBar .
在Android3.0之后,Google对UI导航设计上进行了一系列的改革,其中有一个非常好用的新功能就是引入的ActionBar,他用于取代3.0之前的标题栏,并提供更为丰富的导航效果. 一.添加A ...
- thinking in java知识小记(一)
知识点一(javadoc): 使用javadoc时特别注意选择encoding和charset为utf-8,要不然生成的javadoc会是乱码,命令:javadoc -encoding utf-8 - ...
- 在WIN7系统下用Quartus ii 11.1 NIOS II 11.1 有时候会出现Nios II 的Run as hardware 中报错:Downloading ELF Process failed
nios工程在编译通过后RUN的过程中出现Error Running Nios II Project: ‘Downloading ELF Process failed’问题原因: 1.nios2 cp ...
- nginx使用keepalived实现高可用
环境: 主:linux-node1 110.0.0.137 备:linux-node2 110.0.0.138 VIP: 110.0.0.120 NGINX安装: # rpm -ivh h ...
- Web Service学习笔记(webservice、soap、wsdl、jws详细分析) (转)
Web Service概述 Web Service的定义 W3C组织对其的定义如下,它是一个软件系统,为了支持跨网络的机器间相互操作交互而设计.Web Service服务通常被定义为一组模块化的API ...
- Localdb Attach Problem
在进行code first的迁移时,update-database后默认在App_data文件夹下会新建数据库,如果删除了在使用update-database命令会出现以下错误: Cannot att ...
- SqlBulkCopy的一个例子
public bool InsertAll(IList<NewStockLuoPan> list) { DataTable dt = new DataTable(); dt.Columns ...
- JAVA与JSON的序列化、反序列化
package com.linkage.app.memcache; import java.util.HashMap;import java.util.Map.Entry; import net.sf ...