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. poj3254:基础状压dp

    第二个状压dp 做过的第一个也是放牛问题,两头牛不能相邻 这个题多了一个限制,就是有些位置不能放牛 于是先与处理一下每一行所有不能放牛的状态,处理的过程直接对每一个不能放牛的状态或以下 ac代码: # ...

  2. thinkphp分页时修改last显示标题

    需要修改Page.class.php里lastSuffix为false,这样才能修改last显示标题. 然后就可以设置了 或者直接在方法中声明: $p->lastSuffix = false; ...

  3. Android初学:联系创建Activity

    public class Activity2 extends Activity{ @Override protected void onCreate(Bundle savedInstanceState ...

  4. myeclipse实现Servlet实例(3) 通过继承HttpServlet接口实现

    (1) 在软件公司 90%都是通过该方法开发. //在HttpServlet 中,设计者对post 提交和 get提交分别处理   //回忆 <form action="提交给?&qu ...

  5. 马士兵 Servlet & JSP(1) Servlet (源代码)

    1.HTTP协议基础测试(获取页面源代码) import java.io.BufferedReader; import java.io.IOException; import java.io.Inpu ...

  6. ipython with ubuntu

    在Linux环境下,其实IDE环境配置比较容易配.所以建议用linux做开发. 首选启动终端:Ctrl+Alt+T sudo apt-get update sudo apt-get install p ...

  7. MSSQL Express版本自动备份数据库

    由于Express版本的数据库没有自动备份数据库的功能,所以需要自己搭建好备份功能 一.具体原理: 1.利用SQL备份命令:Backup Database 2.使用sqlcmd执行备份命令 3.使用系 ...

  8. [2011山东省第二届ACM大学生程序设计竞赛]——Identifiers

    Identifiers Time Limit: 1000MS Memory limit: 65536K 题目:http://acm.sdut.edu.cn/sdutoj/problem.php?act ...

  9. spark1.1.0学习路线

          经过一段时间授课,积累下不少的spark知识.想逐步汇总成资料,分享给小伙伴们.对于想视频学习的小伙伴,能够訪问炼数成金站点的<spark大数据平台>课程.每周的课程是原理加实 ...

  10. Android状态栏颜色修改

    android状态栏颜色修改   状态栏颜色的修改在4.4和5.x环境下分别有不同的方式,低于4.4以下是不能修改的.   5.x环境下 方式一,状态栏将显示为纯净的颜色,没有渐变效果 /** * 状 ...