LeetCode- 476 Number Complement(easy)
来源:互联网 发布:linux find 编辑:程序博客网 时间:2024/06/08 19:31
题目:
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.
代码:
1 class Solution { 2 public: 3 int findComplement(int num) { 4 int x = 0, p = 0; 5 while(num){ 6 if(num%2==0) x |= (1 << p); 7 ++p; 8 num/=2; 9 }10 return x;11 }12 };
阅读全文
0 0
- LeetCode- 476 Number Complement(easy)
- LeetCode OJ 476 Number Complement [Easy]
- LeetCode :【Easy】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
- LeetCode 476 Number Complement
- LeetCode 476 : Number Complement
- Leetcode 476 Number Complement
- LeetCode(476): Number Complement
- LeetCode number 476 Number Complement
- leetcode 476 Number Complement C++
- LeetCode 476 Number Complement(位运算)
- 【Mybatis从0到1-009】订单商品模型数据分析
- zzuli 2172 GJJ的日常之购物【dp+优先队列】
- ubuntu14.04更换内核为3.14
- SQL进阶语句
- 三八、前端跨域知识总结
- LeetCode- 476 Number Complement(easy)
- SVN a peg revision is not allowed here 解决办法
- Eclipse使用小技巧
- quartz集群动态配置时间
- 一些团队规范
- java:组合过滤器 将各种流的构造器链接起来 实现多种功能
- 8.21 实习总结 instanceof 与 .getclass()
- 微软消息队列-MSMQ
- Java-数字黑洞 (20)