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: 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 Solution:
Runtime beats 61.45%
完成日期:06/29/2017
关键词:Bit Manipulation
关键点:利用 & 1 判断最右边的bit; 利用 >> 来移动bits;判断num == 1 来终止反转
public class Solution
{
public int findComplement(int num)
{
int res = 0;
int x = 1; for(int i=0; i<32; i++)
{
if((num & 1) == 0) // meaning num's bit is 0, need to flip this bit.
res += x; x = x * 2; // increase the x if(num == 1) // if current num is == 1, meaning all the leading numbers are zeros; stop
break; num = num >> 1; // shift num to right by 1 bit. } return res;
}
}
参考资料:
http://www.cnblogs.com/grandyang/p/6275742.html
LeetCode 算法题目列表 - LeetCode Algorithms Questions List
LeetCode 476. Number Complement (数的补数)的更多相关文章
- 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 ...
- LeetCode 476 Number Complement 解题报告
题目要求 Given a positive integer, output its complement number. The complement strategy is to flip the ...
- 476 Number Complement 数字的补数
给定一个正整数,输出它的补数.补数是对该数的二进制表示取反.注意: 给定的整数保证在32位带符号整数的范围内. 你可以假定二进制数不包含前导零位.示例 1:输入: 5输出: 2解释: 5的 ...
- LeetCode: 476 Number Complement(easy)
题目: Given a positive integer, output its complement number. The complement strategy is to flip the b ...
- 【leetcode】476. Number Complement
problem 476. Number Complement solution1: class Solution { public: int findComplement(int num) { //正 ...
- 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 ...
- [LeetCode&Python] Problem 476. Number Complement
Given a positive integer, output its complement number. The complement strategy is to flip the bits ...
随机推荐
- **********MySql查询方法重要**********
分析问题要分步,查询每一步的结果,最后连起来 例如下面有四张表让你查询 查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录. 第一部,找到课程编号为3-105的记录中学号为109 ...
- Hibernate table schema 的设置与应用
hibernate在实现实体映射时,DB无需强行指定.部署时会较对DB户名和密码,根据用户名以访问的表完成实体映射.如果一个帐号可以访问一个数据库的下多个表,以oracle为例用户user1下面有表t ...
- Linux 命令练习
ls命令 ls就是list的简写,目的是打印当前目录下的清单 格式 ls[选项][目录名] 常用参数 -a –all 列出目录下的所有文件,包括以 . 开头的隐含文件 -l 除了文件名之外,还将文件 ...
- MyEclipse/Eclipse 使用图文详解
引言 某天在群里看到有小伙伴问MyEclipse/Eclipse的一些使用问题,虽然在我看来,问的问题很简单,但是如果对于刚刚学习的人来说,可能使用就不那么友好了.毕竟我在开始使用MyEclipse/ ...
- Poj 1032 Parliament
Parliament Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 19103 Accepted: 8101 Descr ...
- xcode7.3 iTunes Store operation failed解决
使用apploader上传程序 提示:如果您安装了XCode开发环境.在/Applications/XCode.app/Contents/Applications目录中可以找到Application ...
- ThinkPHP中:多个项目共享同一个session问题
使用ThinkPHP3.1.3版本的session时,多个项目同时调试会使得一维数组式的session不够用,导致在A项目登录后台后,在B项目就不用登录后台就可以进入后台操作了. 问题在于他们都调用同 ...
- linux debian 9 配置postgresSQL数据库
#读者注意:本文可以选择不看解释,直接执行每段的0中的代码 (〇):一些概念(可以跳过直接使用(一)0的代码) 1. 客户端:psql.postgreSQL的命令行客户端程序,在终端输入psql进入p ...
- vue-resource传参数到后端,后端取不到数据的问题
先上一段代码: this.$http.post('xxx',{Search_Text:this.search_text}).then(function(response){ // 响应成功回调 thi ...
- python数据结构之栈与队列
python数据结构之栈与队列 用list实现堆栈stack 堆栈:后进先出 如何进?用append 如何出?用pop() >>> >>> stack = [3, ...