zoukankan      html  css  js  c++  java
  • LeetCode 89. 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.


    【题目分析】

    一定长度的gray code序列,就是相邻两个数的二进制形式只有一个bit位不同。

    【思路】

    1. 递归的思路

    求长度为n的gray code,可以先求出长度为n-1的gray code,然后在把n-1长度的gray code序列中的每个数加2的n-1次方,然后逆序加到已有的序列中。

    例如 求n=3的gray code

    n = 2 的gray code为[00,01,11,10]

    序列中的每个数加4,逆序,得到[110,111,101,100]

    两个序列合并:[00,01,11,10,110,111,101,100]即为:[0,1,3,2,6,7,5,4]

    手动写的时候,我们发现其实是一种对称关系

    000
    001
    011
    010  ↑
    --------
    110  ↓
    111
    101
    100

    【java代码1】

     1 public class Solution {
     2     public List<Integer> grayCode(int n) {
     3         int num = 1, temp = n;
     4         while(temp-- > 1) num = num << 1;
     5         
     6         return gray(n, num);
     7     }
     8     
     9     public List<Integer> gray(int n, int num) {
    10         if(n == 0){
    11             List<Integer> list = new ArrayList<Integer>();
    12             list.add(0);
    13             return list;
    14         } else {
    15             List<Integer> list = gray(n-1, num>>1);
    16             int len = list.size();
    17             for(int i = len-1; i >=0; i--) {
    18                 list.add(num+list.get(i));
    19             }
    20             return list;
    21         }
    22     }
    23 }
     2. 位运算

      下面我们把二进制数和Gray码都写在下面,可以看到左边的数异或自身右移的结果就等于右边的数。

    二进制数   Gray码
       000       000
       001       001
       010       011
       011       010
       100       110
       101       111
       110       101
       111       100

        从二进制数的角度看,“镜像”位置上的数即是对原数进行not运算后的结果。比如,第3个数010和倒数第3个数101的每一位都正好相反。假设这两个数分别为x和y,那么x xor (x shr 1)和y xor (y shr 1)的结果只有一点不同:后者的首位是1,前者的首位是0。而这正好是Gray码的生成方法。这就说明了,Gray码的第n个数确实是n xor (n shr 1)。

    【java代码2】
     1 public class Solution {
     2     public List<Integer> grayCode(int n) {
     3         List<Integer> list = new ArrayList<Integer>();
     4         
     5         for(int i = 0; i < 1<<n; i++) {
     6             list.add(i^(i>>1));
     7         }
     8         
     9         return list;
    10     }
    11 }
  • 相关阅读:
    fiddler 抓取 安卓模拟器 https包
    heidiSQL使用简介
    weblogic重启脚本
    svn命令在linux下的使用
    LVS之NAT和DR服务脚本
    LVS之NAT模型、DR模型配置和持久连接
    apache ab压力测试报错apr_socket_recv
    LVS负载均衡模型及算法概述
    Orcale11g单机安装与卸载
    IPC相关的命令
  • 原文地址:https://www.cnblogs.com/liujinhong/p/6280054.html
Copyright © 2011-2022 走看看