Leetcode#476. Number Complement(求补码)
来源:互联网 发布:emergency mode ubuntu 编辑:程序博客网 时间:2024/05/16 09: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.
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.wod
我的代码:
class Solution {public: int findComplement(int num) { int a[100]={0},sum=0,k=0; while(num) { a[k++]=!(num%2); num/=2; } for(int i=k-1;i>=0;i--) sum=sum*2+a[i]; return sum; }};
阅读全文
0 0
- Leetcode#476. Number Complement(求补码)
- [LeetCode]476. Number Complement(数字补码)
- Leetcode-Algorithms 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
- 476. Number Complement [LeetCode]
- [LeetCode]476.Number Complement
- 【LeetCode】 476. Number Complement
- LeetCode 476. Number Complement
- LeetCode 476. Number Complement
- 【LeetCode】476. Number Complement
- 从控制台获取多行不等长数据到一个二维数组
- css BFC 原理
- 线程死锁问题
- 差分约束的做法
- Servlet学习日记(二)——405错误解决及各种http状态码总结
- Leetcode#476. Number Complement(求补码)
- K-means算法的实现原理和分析
- 【译】手把手教你用ngrx管理Angular状态
- kuangbin 最短路 H题(如何将看似拓扑的问题转化成floyd )
- 二叉树有关的高频面试题
- [COGS2600] [FJWC2017] 交错和查询
- java poi excel关于数据碰撞和excel数据追加
- 内省、JavaBean、PropertyDescriptor类、Introspector类、BeanUtils工具包、注解、Rentention、Target、注解的基本属性和高级属性
- java刷题之查漏补缺(六)