Easy-5
来源:互联网 发布:舒柏奇犬 知乎 编辑:程序博客网 时间:2024/06/04 20:52
leetcode 476. Number Complement
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.
AC:
int findComplement(int num) {
int k=num;
int i=0;int sum=1;
while(num>0){
num=num>>1;
i++;
}
for(int j=0;j<i;j++)
{
sum*=2;
}
return sum-1-k;
}
tips: >>右移一位,注意循环变量。
0 0
- Easy-5
- Leetcode--easy系列5
- easy
- Easy
- Easy Search and Replace V1.5
- [Leetcode 5, Easy] Longest Palindromic Substring
- Easy 5 Valid Parentheses(20)
- LeetCode-Easy刷题(5) Valid Parentheses
- How to write a Linux virus in 5 easy steps
- How To Defrag Your Mind In 5 Easy Steps
- Install Firefox 11 in 5 easy steps on Linux
- 2014ACM集训13级PK赛5-Easy Task
- SSH Passwordless Login Using SSH Keygen in 5 Easy Steps
- ZOJ-2969-Easy Task【5th浙江省赛】
- 用java解leetcode上easy题5
- Easy-题目5:237. Delete Node in a Linked List
- 5-10 Saving James Bond - Easy Version (25分)
- Mysql主从复制完美配置,So easy!5分钟搞定!
- Git 学习笔记 —— 一篇文章搞定Git、GitHub的理解和使用
- 文章标题
- 设计模式05:建造者模式
- display:table-cell的应用
- Selenium3 浅尝
- Easy-5
- Django 连接Mysql的方法及常见问题
- python乱码问题一览
- 欢迎使用CSDN-markdown编辑器
- 二叉树中序遍历非递归实现
- c++ 迭代器iterator 和 vector
- 重写BasicButtonUI的paintButtonPressed方法
- 单词记忆方法
- 《网络是怎样连接的》部分总结