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 ...
随机推荐
- DNS没有生效的几个原因
1.记录没有正确添加 请确认你的域名记录是否完全正确的添加.线路类型正确,记录类型正确 2.域名还没有生效 这个情况还会有另外一个现象,就是域名有时候可以ping,有时候不能ping. 这是因为你当地 ...
- Jquery右击显示菜单事件,运用smartMenu插件。
基本格式: 1.引用jquery.smartMenu插件.css样式: <script src="gongju/jquery-1.11.2.min.js" type=&quo ...
- Redis系列二(yum切换为网易163)
这个可能和Redis没有直接的关系... 是我在yum install的时候发现centos的yum实在是太慢,上网查了下.网易163有个yum镜像,为了让CentOS6使用速度更快的YUM更新源,可 ...
- 蓝桥网试题 java 基础练习 数列特征
----------------------------------- Collections.sort(list);是个好东西 但是要学会排列 然后你才能浪 -------------------- ...
- 最近一年多我总结的常用mate标签-常用mate标签
昨天开始上班 ,今天晚上不是太忙 ,来写篇博客了 meta元素共有三个可选属性(http-equiv.name和scheme)和一个必选属性(content),content定义与 http-equ ...
- Javascript前端面试题
在网上看到了一些Javascript的面试题就整理了下来,后续看到再继续补充. 面试题按类型来分,主要涉及到"技术"与"非技术"两大类,技术类别下涉及到的子类别 ...
- vue2重写饿了么
构建 vue有自己的脚手架构建工具vue-cli,使用起来非常方便,使用webpack来集成各种开发便捷工具,比如: 代码热更新,修改代码之后网页无刷新改变,对前端开发来说非常的方便 PostCss, ...
- Git本地操作相关介绍
本地使用git时遇到问题及解决方案总结: 1.git push origin master 后,终端上出现错误信息: push失败,原因多半是因为github上远程仓库中有Reademe.md文件 解 ...
- Swift 2.2 最基本的多线程
昨天晚上苹果召开了发布会,第二天除了知道 iPhone SE 和 IOS9.3 之外,你还记住了什么,这一天还是老样子,继续着我们的Swift的基本学习,但出现了许多的警告,进去看看文档宝宝才知道 S ...
- Java获取http和https协议返回的json数据
现在很多公司都是将数据返回一个json,而且很多第三方接口都是返回json数据,而且还需要使用到http协议,http协议是属于为加密的协议,而https协议需要SSL证书,https是将用户返回的信 ...