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.

这道题给了我们一个数,让我们求补数。通过分析题目汇总的例子,我们知道需要做的就是每个位翻转一下就行了,但是翻转的起始位置上从最高位的1开始的,前面的0是不能被翻转的,所以我们从高往低遍历,如果遇到第一个1了后,我们的flag就赋值为true,然后就可以进行翻转了,翻转的方法就是对应位异或一个1即可,参见代码如下:

解法一:

class Solution {
public:
int findComplement(int num) {
bool start = false;
for (int i = ; i >= ; --i) {
if (num & ( << i)) start = true;
if (start) num ^= ( << i);
}
return num;
}
};

由于位操作里面的取反符号~本身就可以翻转位,但是如果直接对num取反的话就是每一位都翻转了,而最高位1之前的0是不能翻转的,所以我们只要用一个mask来标记最高位1前面的所有0的位置,然后对mask取反后,与上对num取反的结果即可,参见代码如下:

解法二:

class Solution {
public:
int findComplement(int num) {
int mask = INT_MAX;
while (mask & num) mask <<= ;
return ~mask & ~num;
}
};

再来看一种迭代的写法,一行搞定碉堡了,思路就是每次都右移一位,并根据最低位的值先进行翻转,如果当前值小于等于1了,就不用再调用递归函数了,参见代码如下:

解法三:

class Solution {
public:
int findComplement(int num) {
return ( - num % ) + * (num <= ? : findComplement(num / ));
}
};

参考资料:

https://discuss.leetcode.com/topic/74627/3-line-c

https://discuss.leetcode.com/topic/74968/simple-java-one-line-solution

https://discuss.leetcode.com/topic/74642/java-1-line-bit-manipulation-solution

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Number Complement 补数的更多相关文章

  1. LeetCode——Number Complement

    LeetCode--Number Complement Question Given a positive integer, output its complement number. The com ...

  2. Leetcode: Number Complement

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

  3. 【leetcode】476. Number Complement

    problem 476. Number Complement solution1: class Solution { public: int findComplement(int num) { //正 ...

  4. LeetCode#476 Number Complement - in Swift

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

  5. LeetCode_476. Number Complement

    476. Number Complement Easy Given a positive integer, output its complement number. The complement s ...

  6. 2016.5.15——leetcode:Number of 1 Bits ,

    leetcode:Number of 1 Bits 代码均测试通过! 1.Number of 1 Bits 本题收获: 1.Hamming weight:即二进制中1的个数 2.n &= (n ...

  7. LeetCode——Number of Boomerangs

    LeetCode--Number of Boomerangs Question Given n points in the plane that are all pairwise distinct, ...

  8. LeetCode_Easy_471:Number Complement

    LeetCode_Easy_471:Number Complement 题目描述 Given a positive integer, output its complement number. The ...

  9. LeetCode 476. Number Complement (数的补数)

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

随机推荐

  1. [css 揭秘]:CSS揭秘 技巧(二):多重边框

    我的github地址:https://github.com/FannieGirl/ifannie/ 源码都在这上面哦! 喜欢的给我一个星吧 多重边框 问题:我们通常希望在css代码层面以更灵活的方式来 ...

  2. NOIP2017划水崩盘记

    Before-Day1              自信心爆棚,老子一定能拿省一.一天啥也没干,一顿乱奶.敬等明日切T1写暴力. Day 1 哈哈哈,T1是数论,闭眼睛切啊!! ...然后就Gg了,写的 ...

  3. html5 input type="color"边框伪类效果

    html5为input提供了新的类型:color <input type="color" value="#999" id="color" ...

  4. LeetCode-391. 完美矩形(使用C语言编译,详解)

    链接:https://leetcode-cn.com/problems/perfect-rectangle/description/ 题目 我们有 N 个与坐标轴对齐的矩形, 其中 N > 0, ...

  5. c语言程序设计第6周编程作业一(分解质因数)

    分解质因数 题目内容: 每个非素数(合数)都可以写成几个素数(也可称为质数)相乘的形式,这几个素数就都叫做这个合数的质因数.比如,6可以被分解为2x3,而24可以被分解为2x2x2x3. 现在,你的程 ...

  6. Spring学习笔记(1)

    激发pojo的潜能,不会让pojo类继承实现或导入与Spring API相关的任何东西 那么spring怎么装配pojo呢-------->依赖注入(最大好处:松耦合) 耦合具有两面性: 一方面 ...

  7. Software Engineering-HW1

    title: Software Engineering-HW1 date: 2017-09-13 15:41:13 tags: HW --- 阅读随笔 在<徐宥:掉进读书的兔子洞>里面, ...

  8. 团队作业7——第二次项目冲刺(Beta版本)

    Deadline: 2017-12-10 23:00PM,以博客发表日期为准.   评分基准: 按时交 - 有分,检查的项目包括后文的三个方面 冲刺计划安排(单独1篇博客) 七天的敏捷冲刺(每两天发布 ...

  9. java 1.7新特性

    try( ... ){ ... } catch(xxx e){ ... } java1.7特性,叫做try-with-resource,实现了AutoCloseable接口的实例可以放在try(... ...

  10. JAVA_SE基础——13.选择结构语句

    if选择结构 语法: if(条件){ 代码块 } public class Test{ public static void main(String[] args){ int a = 5; if(a ...