LeetCode——Number Complement
LeetCode——Number Complement
Question
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.
具体实现
class Solution {
public:
int findComplement(int num) {
int i = 0;
while (num >= pow(2, i)) {
i++;
}
int value = pow(2, i > 0 ? i : 1) - 1;
return value ^ num;
}
};
LeetCode——Number Complement的更多相关文章
- [LeetCode] Number Complement 补数
Given a positive integer, output its complement number. The complement strategy is to flip the bits ...
- Leetcode: Number Complement
Given a positive integer, output its complement number. The complement strategy is to flip the bits ...
- 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
problem 476. Number Complement solution1: class Solution { public: int findComplement(int num) { //正 ...
- 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 ...
随机推荐
- encodeURI() 的用法
定义和用法 encodeURI() 函数可把字符串作为 URI 进行编码.[通用资源标识符(Uniform Resource Identifier, 简称"URI")] 语法 en ...
- Cannot call sendRedirect() after the response has been committed错误;
Cannot call sendRedirect() after the response has been committed提示信息其实很清楚,如果response已经提交过了,就无法再发送sen ...
- JAVA大数(转)
1.输入 首先要想输入需要先包括: import java.util.*; 我们需要其中的 Scanner类声明的对象来扫描控制台输入. 针对A+B来说: import java.util.*; pu ...
- script跨域之360搜索
思考: 布局: 1,flex元素上下左右居中,内部元素横向排列: div{ /* 100vh = viewport height*/ display: flex; justify-content: c ...
- 2534: Uva10829L-gap字符串
2534: Uva10829L-gap字符串 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 177 Solved: 66[Submit][Statu ...
- 一个有意思的 Java HashSet 问题
昨天,在百度的 java吧 看到有人问关于 HashSet 的问题.下面是他贴出的代码: import java.util.HashSet; public class JavaTest { publi ...
- office 2013 activiate---(run as admin)
win7 office 2013 activiate---(run as admin) empty the garbage in osx rm -rf ~/.Trash
- jquery获取浏览器类型和版本号的方法
$(document).ready(function(){ varbrow=$.browser; varbInfo=""; if(brow.msie){bInfo="Mi ...
- android studio上传项目到github报错Successfully created project 'Demo' on GitHub, but initial commit failed:
今天博主正在愉快地学习在AndroidStudio中使用Git,结果报了下面这个错∑(っ°Д°;)っ: Can't finish GitHub sharing process Successfully ...
- CoreThink开发(十二)更改默认出错异常页防止暴露敏感数据
默认的异常页会打印文件位置,而且是绝对路径,会打印SQL语句,真实上线一定不要用这个默认的,而且关闭trace关闭调试模式也不行. 针对CoreThink1.2 ThinkPHP3.2 这个文件在 A ...