LeetCode——476. Number Complement(二进制,C++)
来源:互联网 发布:windows取消自动更新 编辑:程序博客网 时间:2024/05/16 01:49
题目链接
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: 5Output: 2Explanation: 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: 1Output: 0Explanation: The binary representation of 1 is 1 (no leading zero bits), and its complement is 0. So you need to output 0.
题解:
观察可知要求输出的结果和给定的数之和的形式为111...111,故而只需找到刚好大于的1000....,减1得到11....,再减去原来的数,即为结果。
代码:
class Solution {
public:
int findComplement(int num) {
unsigned int x=1;
while(1)
{
if(x>num)
return x-num-1;
else
x<<=1;
}
}
};
阅读全文
0 0
- LeetCode——476. Number Complement(二进制,C++)
- 【C++】【LeetCode】476. Number Complement
- LeetCode——476. Number Complement
- [LeetCode] Number Complement 二进制反码
- leetcode 476. Number Complement(C语言)
- leetcode 476. Number Complement(C语言)
- LeetCode 476. Number Complement
- leetcode 476. Number Complement
- 【Leetcode】476. Number Complement
- 【Leetcode】476. Number Complement
- LeetCode 476. Number Complement
- LeetCode 476. Number Complement
- LeetCode--476. Number Complement
- LeetCode 476. Number Complement
- 476. Number Complement [LeetCode]
- [LeetCode]476.Number Complement
- 【LeetCode】 476. Number Complement
- LeetCode 476. Number Complement
- 怎么快速把CAD文件转为jpg文件
- C++头文件为什么要加#ifndef #define #endif
- freemarker list map 实例
- 设计模式的六大原则记录
- 关于微信浏览器 设置遮罩层后 onverflow:hidden属性不起作用,仍然导致下层内容可滚动的解决办法
- LeetCode——476. Number Complement(二进制,C++)
- xml的controller解析,通过接口状态去更新页面状态
- 差分放大器
- 自定义surfaceview onmeasure不执行
- Drools 规则引擎----向领域驱动进步(一)
- c# 包含哪些关键字?
- C++正则表达式
- 笨方法学Python 习题 25: 更多更多的练习
- xls数据导入到数据库