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 解答的更多相关文章

  1. LeetCode OJ 89. Gray Code

    题目 The gray code is a binary numeral system where two successive values differ in only one bit. Give ...

  2. [LeetCode] Gray Code 格雷码

    The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...

  3. 【LeetCode】Gray Code

    Gray Code The gray code is a binary numeral system where two successive values differ in only one bi ...

  4. Gray Code

    Gray Code The gray code is a binary numeral system where two successive values differ in only one bi ...

  5. 【leetcode】Gray Code (middle)

    The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...

  6. [LintCode] Gray Code 格雷码

    The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...

  7. 44. Decode Ways && Gray Code

    Decode Ways A message containing letters from A-Z is being encoded to numbers using the following ma ...

  8. LeetCode——Gray Code

    Description: The gray code is a binary numeral system where two successive values differ in only one ...

  9. LeetCode:Gray Code(格雷码)

    题目链接 The gray code is a binary numeral system where two successive values differ in only one bit. Gi ...

随机推荐

  1. <php>PDO用法二

    <?php //造PDO对象 $pdo = new PDO("mysql:dbname=mydb;host=localhost","root"," ...

  2. [iOS] Baritem 添加一项

    不是拖拽,而是在设计栏的属性设置里面.

  3. 5狐网教你从零基础做Firefox os 手机应用开发赚money

    如果你还没有接触过web编程,这里有基础教程教你怎样一步一步学习开发,如果你已经是一个web编程基础的人,那你就很容易将web编程放到手机上,轻松教你移植web应用游戏到Firefox手机应用再发布到 ...

  4. C# 大小写转换

    全部大写: string upper = str.ToUpper() 全部小写: string lower = str.ToLower(); str是需要转换的字符.

  5. 《第一行代码》学习笔记14-UI(3)

    1. (1)所有控件都是直接或间接继承自View,所用的所有布局都是直接或间接继承自ViewGroup的. (2)View是Android中一种最基本的UI组件,可以在屏幕上绘制一块矩形区域,并能响应 ...

  6. ios文件读取(二)

    - (void)viewDidLoad { [super viewDidLoad]; /** *  @brief 获取文件路径 * */ NSString * filePath = [self get ...

  7. jQuery input -> file change事件bug

    由jQuery绑定类型为file的input控件的change事件,发现只能被触发一次,修改方法 --> 原始代码: $input.change(function() { // somethin ...

  8. Render和template?

    Template是一个模板. render = web.template.render('templates/') 这会告诉web.py到你的模板目录中去查找模板.然后把 index.GET改成: 告 ...

  9. vc远程调试启动进程(非attach)

    被调试端设置同attach进程方式的远程调试 代码端,需要在[Project] [Properties] [Configuration Properties] [Debugging].将Debugge ...

  10. system进程启动普通用户进程调研

    system进程启动普通用户进程 关键函数是CreateProcessAsUser 主要思路是先取得目的用户的token,然后用上面的函数启动 1.从explorer中取token BOOL GetT ...