• 476. Number Complement 二进制中的相反对应数


    [抄题]:

    Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation.

    Note:

    1. The given integer is guaranteed to fit within the range of a 32-bit signed integer.
    2. You could assume no leading zero bit in the integer’s binary representation.

    Example 1:

    Input: 5
    Output: 2
    Explanation: The binary representation of 5 is 101 (no leading zero bits), and its complement is 010. So you need to output 2.
    

    Example 2:

    Input: 1
    Output: 0
    Explanation: The binary representation of 1 is 1 (no leading zero bits), and its complement is 0. So you need to output 0.

     [暴力解法]:

    时间分析:

    空间分析:

     [优化后]:

    时间分析:

    空间分析:

    [奇葩输出条件]:

    [奇葩corner case]:

    [思维问题]:

    [一句话思路]:

    1. 每一位都加满,然后作

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

    [画图]:

    [一刷]:

    1. 次方要用Math.pow

    [二刷]:

    [三刷]:

    [四刷]:

    [五刷]:

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

    [总结]:

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

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

    [关键模板化代码]:

    [其他解法]:

    [Follow Up]:

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

     [代码风格] :

    class Solution {
        public int findComplement(int num) {
            //ini
            int i = 0, j = 0;
            
            //while loop
            while (i < num) {
                i += Math.pow(2, j);
                j++;
            }
            
            //return
            return i - num;
        }
    }
    View Code
  • 相关阅读:
    解决vue项目中出现Invalid Host header问题
    vue、uni-app常用的过滤器
    vue让子组件刷新的方法
    工作中 交换机 相关
    XOR Sum in Assembly
    mysql 秒转化为-时分秒
    springboot项目-声明式事务失效
    DOM 元素 classList 的操作方式
    normalize() 和 splitText()
    js 一道面试题,有关函数执行逻辑
  • 原文地址:https://www.cnblogs.com/immiao0319/p/8989702.html
Copyright © 2020-2023  润新知