博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
476. Number Complement 二进制中的相反对应数
阅读量:4354 次
发布时间:2019-06-07

本文共 1278 字,大约阅读时间需要 4 分钟。

[抄题]:

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: 5Output: 2Explanation: 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: 1Output: 0Explanation: 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

 

转载于:https://www.cnblogs.com/immiao0319/p/8989702.html

你可能感兴趣的文章
Educational Codeforces Round 26 D. Round Subset
查看>>
【笔记】数论
查看>>
字典的常用操作
查看>>
C# 创建、部署和调用WebService的简单示例 (转)
查看>>
PHP面向对象(OOP)----访问限制符
查看>>
No qualifying bean of type [java.lang.String] found for dependency: expected
查看>>
人脸检测和识别主页
查看>>
PHP性能优化的五条技巧
查看>>
cocos2d-x注意点
查看>>
开发进度2
查看>>
滑动窗口
查看>>
nyoj-542 试制品 化学方程式 STL map 应用
查看>>
Validation failed for one or more entities while saving changes to SQL Server Database
查看>>
ACM水题
查看>>
Redis实例
查看>>
Android 布局
查看>>
windows下mongodb安装与使用整理
查看>>
EF6与mvc5系列(3):在MVC应用程序中使用EF进行排序,过滤和分页
查看>>
javascript学习
查看>>
第1天:jQuery效果
查看>>