[LintCode] Gray Code

java 5 2016-02-29 13:03
女装

Problem

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, find the sequence of gray code. A gray code sequence must begin with 0 and with cover all 2n integers.
http://mathworld.wolfram.com/GrayCode.html

Example

Given n = 2, return [0,1,3,2]. Its gray code sequence is:

00 - 0
01 - 1
11 - 3
10 - 2

Solution

其实就是找规律,0-01-0132-01326754这样,每个循环(i):

  1. 增加当前res.size()个新数;

  2. 新的循环先在2进制的第(i+1)位加1,同时与之前res所存的数字倒序相加产生新数;

  3. 存入新数,进入下一个循环后更新size。

参考了codesolutiony的算法,简化了一下:

public class Solution {
    public ArrayList<Integer> grayCode(int n) {
        ArrayList<Integer> res = new ArrayList<Integer>();
        res.add(0);
        for (int i = 0; i < n; i++) {
            //每个循环更新size: 1, 3, 7...
            int size = res.size() - 1;
            //i对应最高位是第i+1位,res就增加2^(i+1)个数:(1<<i)+res.get(j)
            //j为倒序循环(size+1)次,会镜像提取上一次循环产生的结果
            //01 - 32: 镜像+2  0132 - 6754: 镜像+4 
            //0 1 3 2 6 7 5 4 - 12 13 15 14 10 11 9 8: 镜像+8
            for (int j = size; j >= 0; j--) {
                res.add((1<<i) + res.get(j));
            }
        }
        return res;
    }
}
女装
文章评论