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;
    }
    }

     [潜台词] :

  • 相关阅读:
    嵌入式设备之web服务器
    动环监控的设备架构设计
    墓碑机制
    淘宝APP消息推送模型
    Service Mesh架构的持续演进 单体模块化 SOA 微服务 Service Mesh
    网关架构演进之路
    理解了云原生,才能正确迎接云时代的到来 | 技术前沿 百度智能云 2019-09-10
    策略模式 VS 状态模式
    权限管理机制支持多种访问控制模型。
    二维码生成服务架构的演进与思考
  • 原文地址:https://www.cnblogs.com/immiao0319/p/9389281.html
Copyright © 2011-2022 走看看