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: 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
分析:本题求一个整型数的二进制补数,即对应二进制位取反。
思路:数拆成二进制,同时进行运算,对有效位取反,再组装成十进制数即可。
JAVA CODE
class Solution {
public int findComplement(int num) {
//将二进制数组(an ... a3 a2 a1 a0)装成十进制。a0+2*(a1+2*(a2+...2*(an+0))) 用递归的思想可以得到很简洁的代码(注意取反操作)。
return (1-num%2)+2*(num==1?0:findComplement(num/2));
}
}
Number Complement的更多相关文章
- 【leetcode】476. Number Complement
problem 476. Number Complement solution1: class Solution { public: int findComplement(int num) { //正 ...
- LeetCode——Number Complement
LeetCode--Number Complement Question Given a positive integer, output its complement number. The com ...
- LeetCode_Easy_471:Number Complement
LeetCode_Easy_471:Number Complement 题目描述 Given a positive integer, output its complement number. The ...
- LeetCode_476. Number Complement
476. Number Complement Easy Given a positive integer, output its complement number. The complement s ...
- LeetCode#476 Number Complement - in Swift
Given a positive integer, output its complement number. The complement strategy is to flip the bits ...
- LeetCode 476. Number Complement
Given a positive integer, output its complement number. The complement strategy is to flip the bits ...
- 476. Number Complement
题目 Given a positive integer, output its complement number. The complement strategy is to flip the bi ...
- LeetCode 476. Number Complement (数的补数)
Given a positive integer, output its complement number. The complement strategy is to flip the bits ...
- 【LeetCode】476. Number Complement (java实现)
原题链接 https://leetcode.com/problems/number-complement/ 原题 Given a positive integer, output its comple ...
随机推荐
- 常用的十大Python开发工具
据权威机构统计,Python人才需求量每日高达5000+,但目前市场上会 Python 的程序员少之又少, 竞争小,很容易快速高薪就业.可能你并不太了解常用的十大Python开发工具都有哪些,现在告诉 ...
- 正则表达式 提取<A>标签
功能用途 主要实现了提取html代码中的a标签和url地址. 示例代码 Regex regex = new Regex("href\\s*=\\s*(?:\"(?<1> ...
- java对文件加锁
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt208 在对文件操作过程中,有时候需要对文件进行加锁操作,防止其他线程访问该文 ...
- 如何解决xshell中无法输入中文的问题
自从安上了xshell以后,用着那叫一个顺手,美中不足的就是一直无法输入中文.不过,既然学习IT,就要习惯英文嘛~直到--我遇到了脚本,写好一个脚本,必要的注释是少不了的,但是作为一个英文渣渣,我真的 ...
- HTML5 贝塞尔绘画 桃心
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 第二次项目冲刺(Beta阶段)5.25
1.提供当天站立式会议照片一张 会议内容: ①检查前一天的任务情况. ②制定新一轮的任务计划. ③对这几日的冲刺进行总结. 2.每个人的工作 (1)工作安排 队员 今日进展 明日安排 王婧 #63(完 ...
- 团队作业8——第二次项目冲刺(Beta阶段)5.27
1.当天站立式会议照片 会议内容: 本次会议为第七次会议 本次会议在陆大楼2楼召开,本次会议内容: ①:检查总结上次任务完成情况 ②:安排今天的分工 ③:对昨天的问题进行讨论 2. 每个人的工作 (有 ...
- 团队作业8——第二次项目冲刺(Beta阶段)博客汇总
一.冲刺计划安排 http://www.cnblogs.com/teamworkers/p/6875742.html 二.七天冲刺汇总 http://www.cnblogs.com/teamworke ...
- 团队作业8----第二次项目冲刺(beta阶段)5.21
1. 开会 会议内容:1.总结昨天的任务工作 2. 由潘益靖同学对当天的代码修改及功能的改善做个简单的阐述 3.每个人对项目的发表一些意见及建议 4.组长分配任务 每日讨论图片(拍摄者小组成员 武健男 ...
- 201521123053《Java课程设计》第十四周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多数据库相关内容. 知识点: 创建表的命令有若干行,如果中间某行输入错误,不能修改:可以使用记事本现将命令输入,然后复制粘贴到mys ...