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 ...
随机推荐
- oracle客户端plsql设置字符集
感谢一个新朋友的到来,我帮他的过程中有好些东西都不怎么想的起来了,所以从现在起我需要记录下每一点一滴, 因为我觉得写下来的东西才不会丢,而且欢迎以后的朋友到来. 使用plsql查数据的时候有时候中文会 ...
- 国外支付PayPal
PayPal官网https://www.paypal.com/ PayPal沙箱https://www.sandbox.paypal.com/signin?country.x=US&local ...
- 如何解决conda install:command not found问题
每次运行conda相关代码之前先做一遍source ~/.bashrc.即可
- 一、js的数据类型
一.数据类型 ECMAScript中有5种简单数据类型:Undefined.Null.Boolean.Number和String.还有一种复杂数据类型--Object.ECMAScript不支持任何创 ...
- 初识Hibernate之理解持久化类
上一篇文章我们简单介绍了Hibernate相关的一些最基本的文件及其作用,并在最后完整的搭建了Hibernate的运行环境,成功的完成了与数据库的映射.但是至于其中的一些更加细节的地方并没有 ...
- ExtJS配置与入门项目创建
Sencha Cmd下载:http://cdn.sencha.com/cmd/6.5.2/jre/SenchaCmd-6.5.2-windows-64bit.zip ExtJS-6.2.0下载:htt ...
- IFrame父页面和子页面的交互
现在在页面里面用到iframe的情况越来越少了,但有时还是避免不了,甚至这些页面之间还需要用js来做交互,那么这些页面如何操作彼此的dom呢?下面将会逐步介绍. 1.父页面操作子页面里面的dom 下面 ...
- CPU 分类
Single Core :单核CPU Dual Core :双核CPU Quad Core :四核CPU Hexa Core :六核CPU Octa Core :八核CPU 参考: htt ...
- vue2组件之select2调用
目前,项目中使用了纯前端的静态项目+RESTFul接口的模式.为了更好的对数据进行操作,前端使用了vue2的mvvm功能,但是由于不是单页面应用,所以,并没有涉及到其它的如vue-route等功能,也 ...
- J2EE走向成功路-02-Struts2 配置(Maven)
在上一篇中,介绍了使用jar包搭建Struts2框架,这一篇中来介绍一下在Maven环境下搭建Struts2框架,主要为配置Maven. 1.下载Maven,官网:http://maven.apach ...