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.

链接: http://leetcode.com/problems/gray-code/

题解:

求灰度值。利用定义来做就可以了。比如 00, 01, 11, 10的下一个灰度值为  000, 001, 011, 010 与   prefix 1再逆序的值 110, 111, 101, 100的和, 就是  000, 001, 010, 011, 110, 111, 101, 100。

public class Solution {
    public List<Integer> grayCode(int n) {
        List<Integer> res = new ArrayList<Integer>();
        if(n < 0)
            return res;
        if(n == 0) {
            res.add(0);
            return res;
        }
        
        List<Integer> last = grayCode(n - 1);
        res.addAll(last);
        int prefix = (1 << n - 1);
        
        for(int i = last.size() - 1; i >= 0; i--) 
            res.add(last.get(i) + prefix);
        
        return res;
    }
}

二刷:

Java:

Time Complexity - O(2n), Space Complexity - O(2n)

public class Solution {
    public List<Integer> grayCode(int n) {
        List<Integer> res = new ArrayList<>();
        if (n < 0) {
            return res;
        }
        if (n == 0) {
            res.add(0);
            return res;
        }
        List<Integer> last = grayCode(n - 1);
        res.addAll(last);
        int prefix = 1 << (n - 1);
        for (int i = last.size() - 1; i >= 0; i--) {
            res.add(prefix + last.get(i));
        }
        return res;
    }
}

也可以用类似memorization来降低space complexity = O(1),不考虑结果集的话

public class Solution {
    public List<Integer> grayCode(int n) {
        List<Integer> res = new ArrayList<>();
        res.add(0);
        for (int i = 0; i < n; i++) {
            int prefix = 1 << i;
            for (int j = res.size() - 1; j >= 0; j--) {
                res.add(prefix + res.get(j));
            }
        }
        return res;
    }
}

三刷:

方法和上面一样。

Java:

Recursive:

Time Complexity - O(2n), Space Complexity - O(2n)

public class Solution {
    public List<Integer> grayCode(int n) {
        List<Integer> res = new ArrayList<>();
        if (n <= 0) return Collections.singletonList(0);
        List<Integer> last = grayCode(n - 1);
        res.addAll(last);
        int prefix = 1 << (n - 1);
        for (int i = last.size() - 1; i >= 0; i--) res.add(prefix + last.get(i));
        return res;
    }
}

Iterative:

public class Solution {
    public List<Integer> grayCode(int n) {
        List<Integer> res = new ArrayList<>();
        res.add(0);
        for (int i = 0; i < n; i++) {
            for (int j = res.size() - 1; j >= 0; j--) {
                res.add((1 << i) + res.get(j));
            }
        }
        return res;
    }
}

Reference:

https://leetcode.com/discuss/2978/what-solution-gray-code-problem-extra-space-used-recursion

https://leetcode.com/discuss/24634/an-accepted-three-line-solution-in-java

https://en.wikipedia.org/wiki/Gray_code

原文地址:https://www.cnblogs.com/yrbbest/p/4437150.html