zoukankan      html  css  js  c++  java
  • Leetcode-Gray Code

    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.

    Analysis:

    For num = n, suppose we know a gray sequence for number n-1, we then can construct a sequence for n like this:

    1. add 0 in front of the n-1 sequence.

    2. add 1 in front of the reverse of the n-1 sequence.

    Because the reverse of a valid gray sequence also satisfy the rule of the gray sequence, except that ends with 0. For example,

    for num==2:

    00,01,11,10

    for num==3

    000,001,011,010,     110,111,101,100.

    Change the rule to integer number, then we have:

    1. add n-1 sequence to the list.

    2. reversely add (each int in n-1 sequence+base), where base==2^n.

    Solution:

     1 public class Solution {
     2     public List<Integer> grayCode(int n) {
     3         List<Integer> res = new ArrayList<Integer>();
     4         res.add(0);
     5         if (n==0){            
     6             return res;
     7         }
     8         res.add(1);
     9         if (n==1) return res;       
    10         
    11         int base = 1;
    12         for (int i=2;i<=n;i++){
    13             base = base*2;
    14             List<Integer> newRes = new ArrayList<Integer>();
    15             newRes.addAll(res);
    16             
    17             for (int j=res.size()-1;j>=0;j--){
    18                 int temp = res.get(j)+base;
    19                 newRes.add(temp);
    20             }
    21             res = newRes;
    22         }
    23         return res;
    24         
    25     }
    26 }

     NOTE: We actually does not need to create a new list for every n, since we only need to add the reverse n-1 sequence to the n-1 sequence to get n sequence.

     1 public class Solution {
     2     public List<Integer> grayCode(int n) {
     3         List<Integer> res = new ArrayList<Integer>();
     4         res.add(0);
     5         if (n==0){            
     6             return res;
     7         }
     8         res.add(1);
     9         if (n==1) return res;       
    10         
    11         int base = 1;
    12         for (int i=2;i<=n;i++){
    13             base = base*2;
    14             int end = res.size()-1;
    15             for (int j=end;j>=0;j--){
    16                 int temp = res.get(j)+base;
    17                 res.add(temp);
    18             }
    19         }
    20         return res;
    21         
    22     }
    23 }
  • 相关阅读:
    微软WP7本地数据库之Sqlite编程技巧(转)
    AutoResetEvent详解
    桥接模式的简单分析
    解决VS2008 调试启动特别慢
    软件概要设计
    解决windows8不能安装ZUNE的问题
    CDATA的对特殊字符作用说明
    DataTable对象在内存中的使用(二)
    DataTable对象在内存中的使用(一)
    关于MVC3 CODE FIRST的安装
  • 原文地址:https://www.cnblogs.com/lishiblog/p/4100957.html
Copyright © 2011-2022 走看看