Leetcode 89-格雷编码

题解转载自Krahets
Leetcode 89-格雷编码
Leetcode 89-格雷编码

class Solution {
    public List<Integer> grayCode(int n) {
        List<Integer> res = new ArrayList<Integer>() {{ add(0); }};
        int head = 1;
        for (int i = 0; i < n; i++) {
            for (int j = res.size() - 1; j >= 0; j--)
                res.add(head + res.get(j));
            head <<= 1;
        }
        return res;
    }
}
上一篇:javaweb-青橙项目-14-89


下一篇:奖学金