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.
分析
将int的二进制表示(不包括前缀0)中所有0变为1,1变为0
解答
解法1:(我)(11ms√)
例1:
5: 00000000000000000000000000000101
~5: 11111111111111111111111111111010 (补码形式,原码是10000000000000000000000000000110,值为-6)
-23: 11111111111111111111111111111000 (补码形式,原码是10000000000000000000000000001000,值为-23)
~5-(-23): 00000000000000000000000000000010 (值为2)
例2:
231-1: 01111111111111111111111111111111
~(231-1): 10000000000000000000000000000000 (值为-231)
-231: 10000000000000000000000000000000
~(231-1)-(-231): 00000000000000000000000000000000 (值为0)
注:此处(2<sup>31</sup>-1)-(-2<sup>31</sup>)不能写为(231-1)+231,因为int中正数231超出最大值范围,会被解析成231-1,而负数(-231)没有超出最小值范围
int的最大值:01111111111111111111111111111111 (值为231-1)
int的非最小值:11111111111111111111111111111111 (值为-(231-1) )
int的最小值:10000000000000000000000000000000 (值为-231)(特殊:使用以前的-0的补码来表示, 所以-231并没有原码和反码表示)
public class Solution {
public int findComplement(int num) {
return ~num - (int)-Math.pow(2,32-Integer.numberOfLeadingZeros(num));
}
}
解法2:使用系统内置函数Integer.highestOneBit()(13ms)
Integer.highestOneBit() 返回一个int值:如果i具有'1'位,则返回值具有1个'1'位,其位置即是i的最高位(最左边)的'1'位的位置;如果i不具有'1'位,则i=0,返回0。例:Integer.highestOneBit(5) = 4,因为5的二进制表示为101,返回值的二进制表示为100
例1:
5: 00000000000000000000000000000101
~5: 11111111111111111111111111111010
a: 00000000000000000000000000001000 (a=Integer.highestOneBit(5) << 1
)
~5+a: 00000000000000000000000000000010 (值为2)
例2:
231-1: 01111111111111111111111111111111
~(231-1): 10000000000000000000000000000000 (值为-231)
a: 10000000000000000000000000000000
~(231-1)+a: 00000000000000000000000000000000 (值为0)
public class Solution {
public int hammingDistance(int x, int y){
String str = Integer.toBinaryString(x ^ y);//或Integer.toString(x ^ y , 2)
String str2 = str.replaceAll("1","");
return str.length() - str2.length();
}
}
476. Number Complement的更多相关文章
- 【leetcode】476. Number Complement
problem 476. Number Complement solution1: class Solution { public: int findComplement(int num) { //正 ...
- 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 bits ...
- 【LeetCode】476. Number Complement (java实现)
原题链接 https://leetcode.com/problems/number-complement/ 原题 Given a positive integer, output its comple ...
- LeetCode 476 Number Complement 解题报告
题目要求 Given a positive integer, output its complement number. The complement strategy is to flip the ...
- [LeetCode&Python] Problem 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 ...
- LeetCode: 476 Number Complement(easy)
题目: Given a positive integer, output its complement number. The complement strategy is to flip the b ...
随机推荐
- 在DataTable数据类型最后增加一列,列名为“Column”,内容都为“AAA”
DataTable dt = new DataTable(); dt.Columns.Add("Column", typeof(string)); foreach (DataRow ...
- Pomelo术语解释
gate服务器 一个应用的gate服务器,一般不参与rpc调用,也就是说其配置项里可以没有port字段,仅仅有clientPort字段,它的作用是做前端的负载均衡.客户端往往首先向gate服务器发出请 ...
- Bagging决策树:Random Forests
1. 前言 Random Forests (RF) 是由Breiman [1]提出的一类基于决策树CART的Bagging算法.论文 [5] 在121数据集上比较了179个分类器,效果最好的是RF,准 ...
- java连接ms sql server各类问题解析
首先先来说下使用微软自己开发的架包进行ms sql server数据库的连接时,sql 2000与sql 2005的连接方式略有不同: 1.首先驱动不一样,sql 2000的连接驱动包有三个,分别是: ...
- WPF开发进阶 - Fody/PropertyChanged(一)
INotifyPropertyChanged 在WPF MVVM模式开发中,实现INotifyPropertyChanged的ViewModel是非常重要且常见的类: public class Mai ...
- 光荣与梦想 | XMove动作捕捉系统(一)
XMove是我和几个死党从2010年开始开发的一套人体动作捕捉系统,软硬件全部自行开发,投入了大量的精力,历经三年,发展四个版本. 今年春节回到老家,翻出了2011年春节时焊电路用过的松香和和硬盘角落 ...
- requireJS的初步掌握(二)
前面我们讲述了requireJS的一些认知和优点,==>http://www.cnblogs.com/wymbk/p/6366113.html 这章我们主要描述的是requireJS的一些常用的 ...
- GIS制图课程目录
由于更新次序跳跃式更新,因此很有必要整理一下全书目录,并将会按照实际学习的顺序进行更新. [前言] GIS制图课程前言 [理论篇] 理论篇-地图学与GIS制图的基础理论(一) 理论篇-地图学与GIS制 ...
- Java线程中yield()的用法
Thread.yield()方法的作用:暂停当前正在执行的线程,并执行其他线程.(可能没有效果) yield()让当前正在运行的线程回到可运行状态,以允许具有相同优先级的其他线程获得运行的机会.因此, ...
- 简述public private protected internal修饰符的访问权限
public 关键字是类型和类型成员的访问修饰符.公共访问是允许的最高访问级别.对访问公共成员没有限制. protected 关键字是一个成员访问修饰符.受保护成员在它的类中可访问并且可由派生类访问. ...