[LeetCode] Number Complement 补数
Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation.
Note:
- The given integer is guaranteed to fit within the range of a 32-bit signed integer.
- 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 补数的更多相关文章
- LeetCode——Number Complement
LeetCode--Number Complement Question Given a positive integer, output its complement number. The com ...
- Leetcode: Number Complement
Given a positive integer, output its complement number. The complement strategy is to flip the bits ...
- 【leetcode】476. Number Complement
problem 476. Number Complement solution1: class Solution { public: int findComplement(int num) { //正 ...
- LeetCode#476 Number Complement - in Swift
Given a positive integer, output its complement number. The complement strategy is to flip the bits ...
- LeetCode_476. Number Complement
476. Number Complement Easy Given a positive integer, output its complement number. The complement s ...
- 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 ...
- LeetCode——Number of Boomerangs
LeetCode--Number of Boomerangs Question Given n points in the plane that are all pairwise distinct, ...
- LeetCode_Easy_471:Number Complement
LeetCode_Easy_471:Number Complement 题目描述 Given a positive integer, output its complement number. The ...
- LeetCode 476. Number Complement (数的补数)
Given a positive integer, output its complement number. The complement strategy is to flip the bits ...
随机推荐
- ORACLE 监听
今天来学习一下监听的相关内容,昨晚被老大问了两个关于监听很简单的问题,但是却吞吞吐吐回答,而且有一个问题还答错了,刚刚查了下资料,才发现"驴头对了马嘴",哭笑不得. 一.监听(li ...
- Leetcode 28——Implement strStr()
Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...
- JavaScript之隐式类型转换
布尔操作符(!.&&.||) 当使用 条件判断语句(if...else) 以及 布尔操作符(!.&&.||) 时,会调用Boolean()进行隐式类型转换 转换为fal ...
- 【福大软工】 W班级总成绩排名2
评分链接: 选题报告 结对第二次作业 需求分析 随堂测试 总分排名:
- Linux下vim上编辑实现进度条
1.效果展示: 进度条,先来看一个效果: 这是进度结果,模拟实现了进度条的前进.百分比的现实.以及稍微的动画特效. 2.原理描述: 因为Linux系统下的输出有缓存,如果及时刷新显示,就可以得到我们想 ...
- 《高级软件测试》11.16.Jira使用说明的撰写和操作视频的录制
今日任务完成情况如下: 小王:完成了测试管理工具jira的使用手册中,基本情况介绍.下载安装部分的撰写工作:小高:参考官方手册,结合自己的实际使用体会,对jira的基本组成及其工作流程进行了介绍:小陈 ...
- Spring-Data-JPA整合MySQL和配置
一.简介 (1).MySQL是一个关系型数据库系统,是如今互联网公司最常用的数据库和最广泛的数据库.为服务端数据库,能承受高并发的访问量. (2).Spring-Data-Jpa是在JPA规范下提供的 ...
- .NET Core装饰模式和.NET Core的Stream
该文章综合了几本书的内容. 某咖啡店项目的解决方案 某咖啡店供应咖啡, 客户买咖啡的时候可以添加若干调味料, 最后要求算出总价钱. Beverage是所有咖啡饮料的抽象类, 里面的cost方法是抽象的 ...
- nyoj VF
VF 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 Vasya is the beginning mathematician. He decided to make ...
- zookeeper入门系列 : 分布式事务
上一章我们了解了zookeeper到底是什么,这一章重点来看zookeeper当初到底面临什么问题?而zookeeper又是如何解决这些问题的? 实际上zookeeper主要就是解决分布式环境下的一致 ...