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.

    Example 1:

    Input: 2
    Output: [0,1,3,2]
    Explanation:
    00 - 0
    01 - 1
    11 - 3
    10 - 2
    
    For a given n, a gray code sequence may not be uniquely defined.
    For example, [0,2,3,1] is also a valid gray code sequence.
    
    00 - 0
    10 - 2
    11 - 3
    01 - 1

     [暴力解法]:

    时间分析:

    空间分析:

     [优化后]:

    时间分析:

    空间分析:

    [奇葩输出条件]:

    [奇葩corner case]:

    [思维问题]:

    [英文数据结构或算法,为什么不用别的数据结构或算法]:

    [一句话思路]:

    [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

    [画图]:

    [一刷]:

    <<左乘右除,不允许直接写2n

    [二刷]:

    [三刷]:

    [四刷]:

    [五刷]:

      [五分钟肉眼debug的结果]:

    [总结]:

    就是背:i < 2n时,i ^ i/2 异或是作差,不是01

    [复杂度]:Time complexity: O(n) Space complexity: O(n)

    [算法思想:迭代/递归/分治/贪心]:

    [关键模板化代码]:

    [其他解法]:

    [Follow Up]:

    [LC给出的题目变变变]:

     [代码风格] :

     [是否头一次写此类driver funcion的代码] :

    class Solution {
        public List<Integer> grayCode(int n) {
        List<Integer> result = new LinkedList<>();
        for (int i = 0; i < 1<<n; i++) result.add(i ^ i>>1);
        return result;
    }
    }

     [潜台词] :

  • 相关阅读:
    收藏夹
    获取某个元素在页面上的偏移量
    React多行文本溢出处理(仅针对纯文本)
    react
    CDN初学搭建(ats)
    linux查看cpu、内存、版本信息
    MySQL5.6版本性能调优my.cnf详解
    How to install cacti on centos 6
    win10安装.net3.5 报错解决
    CentOS6.5安装Cacti统计图乱码解决
  • 原文地址:https://www.cnblogs.com/immiao0319/p/9389281.html
Copyright © 2011-2022 走看看