LeetCode-476. Number Complement-思路详解-C++
来源:互联网 发布:mac子弹头口红价格 编辑:程序博客网 时间:2024/06/13 23:38
题目:
Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation.
Note:
1,The given integer is guaranteed to fit within the range of a 32-bit signed integer.
2,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,给定的数的范围在32位有符号整数
2,假设没有前导0.即5的二进制就是101。
思路
按位取反,即可。
代码
class Solution { public: int findComplement(int num) { long int flag = 1; long int res = 0; while(flag && flag <= num){ int t = flag# if(t == 0){ res |= flag; } flag = flag << 1; } return res; } };
0 0
- LeetCode-476. Number Complement-思路详解-C++
- 【C++】【LeetCode】476. 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
- LeetCode 476. Number Complement
- 【LeetCode】476. Number Complement
- 配置Log4j
- 微信小程序—下拉刷新上拉加载
- 获取组件当中某个Drawable所在的坐标范围
- POJ - 1185 炮兵阵地
- [Leetcode] Remove Duplicates from Sorted List II
- LeetCode-476. Number Complement-思路详解-C++
- 共享动画的实现(AndroidL及以上)
- 设计模式之外观模式
- [BZOJ1414][ZJOI2009]对称的正方形(manacher+单调栈+二分)
- Java多态
- profile=epp.package.java, phase=org.eclipse.equinox.internal.p2.engine.phases.Collect, operand=, act
- 常用的字符串函数
- 设计模式【2】-对象行为型-状态模式
- jquery 鼠标经过行变色