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 ...
随机推荐
- EF 更新操作 lambda解释+=
我曾写过一个EF批量更新.删除的博客,后来操作的过程中经常遇到更新字段,但是要在原来的基础上计算的情况,我就先去获取一遍数据然后再计算,最后再去更新,显然这个操作是很复杂的 var guest = d ...
- linux ftp 上传与下载命令解析
month=`date -d "last month" +"%Y%m"` year=`date +"%Y"` rm /home/yourDi ...
- input 和 button 的 border-box 模型和 IE8 错位
用 input 和 button 时出现了几个奇怪的现象,先放几个 input 和 button CSS: * { margin:; padding:; } input,button { width: ...
- 全局安装了express框架,但是无法使用express指令的问题
错误截图: 产生这个错误的原因是:我安装的是express4版本,需要安装express-generator才能使用express命令 将express-generator安装后就都解决了:
- SQL架构信息读取
--架构: select * from information_schema.SCHEMATA --表: select table_name from information_schema.table ...
- python基础之练习题(一)
1.执行 Python 脚本的两种方式 python test.py chmod +x test.py && ./test.py 2.简述位.字节的关系 二进制位(bit)是计算机存储 ...
- Acheron一期SVN地址
svn://10.0.0.100/project/Acheron/2.0/SourceCode tailf 命令 http://web2py.com/books/default/chapter/29/ ...
- Linux学习笔记(3)linux服务管理与启停
一.LINUX 系统服务管理 1.RHEL/OEL 6.X及之前 service命令用于对系统服务进行管理,比如启动(start).停止(stop).重启(restart).查看状态(status)等 ...
- crm--业务点详细概述
一.CRM简介:(为什么开发CRM) 此项目主要供自己的公司使用,原来因为公司人员较少,人员管理考勤等都用excel保存.但是现在因为公司人员,以及部门增多,为了方便管理 ,供销售人员使用内部系统更方 ...
- Cannot format given Object as a Date
这个小错挺有意思的,记录一下 导出Excel的时候,同事直接用 format …… 前提:数据库中该字段是 Timestamp ---- 2016-06-20 22:49:02.967 写个测试说明一 ...