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 ...
随机推荐
- LinkedHashMap遍历
第一种: Map map = new HashMap(); Iterator iter = map.entrySet().iterator(); while (iter.hasNext()) { Ma ...
- loadrunner:web services接口测试
本文以实例讲解web services接口测试操作,内容包括:脚本生成.参数化和接口与接口间的取值关联操作. 网站"http://www.webxml.com.cn/zh_cn/web_se ...
- HDU5057(分块)
Argestes and Sequence Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- HTML5 & CSS3 初学者指南(4) – Canvas使用
介绍 传统的HTML主要用于文本的创建,可以通过<img>标签插入图像,动画的实现则需要第三方插件.在这方面,传统的HTML极其缺乏满足现代网页多媒体需求的能力.HTML5的到来,带来了新 ...
- java实现简单的单点登录_转
摘要:单点登录(SSO)的技术被越来越广泛地运用到各个领域的软件系统当中.本文从业务的角度分析了单点登录的需求和应用领域:从技术本身的角度分析了单点登录技术的内部机制和实现手段,并且给出Web-SSO ...
- PHP变量处理之serialize
官方定义: string serialize ( mixed $value ) serialize() 返回字符串,此字符串包含了表示 value 的字节流,可以存储于任何地方.这有利于存储或传递 P ...
- STM32单片机学习心得——概述
我校的课程真是跟不上时代发展,甚至还在教授8051/8052单片机的内容,于是不甘寂寞的我就自己踏入了STM32单片机的坑-- 首先,我现在大二,刚学完模拟电子技术,还没有学习数字电路技术,于是自学单 ...
- DB2 表空间监控
默认DB2 缓冲池信息监控是OFF, 需要开启(DB2表空间是由缓冲池分配的) CollBufferpool : ============ The CollBufferpool collector c ...
- 关于Test--Pattern Generator IP核的测试
关于Test--Pattern Generator IP核的测试 1.Test--Pattern Generator 功能介绍 生成24-bit RGB视频流,此IP核可以用于系统测试,不需要先在片上 ...
- Vue.js的环境搭建
vue这个新的工具,确实能够提高效率,vue入门的精髓:(前提都是在网络连接上的情况下) 1.要使用vue来开发前端框架,首先要有环境,这个环境要借助于node,所以要先安装node,借助于node里 ...