leetcode.476. Number Complement
来源:互联网 发布:网络传播营销策划案 编辑:程序博客网 时间:2024/06/16 05:20
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: 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.
Solution
获取num的最大位的数值Tmax,取num与Tmax的异或,即补码。
int findComplement(int num) { int mask = num; mask |= mask >> 1; mask |= mask >> 2; mask |= mask >> 4; mask |= mask >> 8; mask |= mask >> 16; return mask ^ num;}
阅读全文
0 0
- 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
- LeetCode 476. Number Complement
- 【LeetCode】476. Number Complement
- leetcode 476. Number Complement
- [LeetCode]476. Number Complement
- LeetCode 476. Number Complement
- 【LeetCode】476. Number Complement
- gcc链接阶段
- (转)ProgressDialog使用总结
- CSV常用命令
- 13 明白了这点才能拿高薪
- Kotlin语法(八)-方法和lambda表达式
- leetcode.476. Number Complement
- 删除作业,执行带参作业
- HeadZoomScrollView下拉放大的图片松开回弹
- Qt 自定义 滚动条 样式
- Oracle中的AS和IS的不同及使用
- Vagrant+Virtual+HomeStead Laravel环境搭建
- 安卓PathMeasure的简单用法
- maven与springMVC之HttpMessageConverter解析json
- Android给CardView加边框以及水波纹效果