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

  • 相关阅读:
    Azure 2 月新公布
    协合新能源康大海:我们每多发一度电,就为蓝天白云多贡献了一份力量
    Azure:陪伴你们,是我最长情的告白
    微软加速器上海首期启航,拓展云端智慧创新
    Azure杯年会Cosplay大赛,速来围观!
    Azure 1 月新公布
    Azure进阶攻略 | VS2015和Azure,想要在一起其实很容易
    Azure进阶攻略丨如何驾驭罢工的Linux虚机网卡?
    Azure 12 月新公布
    利用枚举找到列表中重复元素的索引
  • 原文地址:https://www.cnblogs.com/yrbbest/p/4437150.html
Copyright © 2011-2022 走看看