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. 20165226 2017-2018-3 《Java程序设计》第5学习总结

    20165226 2017-2018-3 <Java程序设计>第5周学习总结 教材学习内容总结 第七章 内部类与异常类 匿名类创建对象: new Bank() { 匿名类的类体 }: 异常 ...

  2. 【Spring源码深度解析学习系列】容器的基础XmlBeanFactory(二)

    一.配置文件封装 Spring的配置文件读取是通过ClassPathResource进行封装的,如new ClassPathResource("test.xml"),那么Class ...

  3. 2017-2018-1 Java演绎法 第六七周 作业

    团队任务:修改完善<需求规格说明书>等 团队组长:袁逸灏 本次编辑:刘伟康 修改完善上周提交的需求规格说明书 [markdown 链接] [pdf 链接] 不足之处:仅就现在的问题来看,结 ...

  4. fs输出文件目录

    var http = require("http"); var fs = require("fs"); var server = http.createServ ...

  5. 【学习笔记】windows安装jhipster踏坑记录

    序: 入职新公司第二天了,本来第一天是配置环境来着,配了一下午也没搞成那个jhipster的安装,每次以为应该正常的时候都是不对,yo是yeoman的指令,但是我是使用yarn管理的yeoman 纠结 ...

  6. 如何书写高效的css样式

    如何书写高效的css样式? 有以下四个关键要素: 1.高效的css 2.可维护的css 3.组件化的css 4.hack-free  css 书写高效的css: 1.使用外联样式替代行间样式或内嵌样式 ...

  7. mycat入门_介绍与安装

    利用闲暇时间接触了下mycat. 一.介绍 1.概述: 国内最活跃的.性能最好的开源数据库中间件,可以理解为数据库和应用层之间的一个代理组件. 2.作用: 读写分离.分表分库.主从切换. 3.原理: ...

  8. JAVA_SE基础——28.封装

    黑马程序员blog... 面向对象三大特征:1. 封装2. 继承3  多态. 今天我们先学习第一大特征,封装. 封装:是指隐藏对象的属性和实现细节,仅对外提供公共访问方式. 好处:     1. 将变 ...

  9. Python-进程与线程理论基础-Day10

    进程与线程理论基础 1.背景知识 理论基础: 一 操作系统的作用: 1:隐藏丑陋复杂的硬件接口,提供良好的抽象接口 2:管理.调度进程,并且将多个进程对硬件的竞争变得有序 二 多道技术: 1.产生背景 ...

  10. python 字符串的方法

    capitalize() 把字符串的第一个字符改为大写 casefold() 把整个字符串的所有字符改为小写 center(width) 将字符串居中,并使用空格填充至长度 width 的新字符串 c ...