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 ...
随机推荐
- Firebug及YSlow简介与使用图文详解
Firebug本是Firefox浏览器下一个出色的网页设计插件,随着浏览器的发展,Firebug也推出了支持IE.Opera.Chrome的Firebug Lite.凭借Firebug的出色代码调试功 ...
- Angularjs^1.2.9 搜索关键字高亮显示
需求分析: 根据关键字搜索网页内容,并且高亮显示内容中的关键字细节分析: 1.每次执行搜索操作,需清空上一次结果 2.需区分html标签和正常文本内容,否则为关键字添加样式以后会出现标签内容被显示的情 ...
- Android开发系列之屏幕密度和单位转换
由于Android的开源性,所以目前市面上面Android手机的分辨率特别多,这样的话就给我适配带来了一定的难度.要想做好适配,我们首先应该明白什么是分辨率.PPI.屏幕大小等概念,还有在不同的屏幕密 ...
- CodeForces758D
D. Ability To Convert time limit per test:1 second memory limit per test:256 megabytes input:standar ...
- Linux 服务器设置成支持中文
Linux 服务器设置成支持中文 由于服务器默认是不支持中文的.所以一般需要单独设置一下. 检查本机已有的语言包 locale -a 默认是没有中文的,所以会显示: C C.UTF-8 POSIX e ...
- Hadoop权威指南:通过distcp并行复制
Hadoop权威指南:通过distcp并行复制 distcp是一个分布式复制程序,改程序可以从Hadoop文件系统间复制大量数据,也可以将大量的数据复制到Hadoop中 distcp的典型应用是在两个 ...
- ADO.NET 防止SQL注入
规避SQL注入 如果不规避,在黑窗口里面输入内容时利用拼接语句可以对数据进行攻击 如:输入Code值 p001' union select * from Info where '1'='1 //这样可 ...
- Xcode自带iOS测试方法
在说Xcode自带测试方法前先讲下程序在内存中的空间划分, 一般可分为5个部分: #1. BSS段, 存放未初始化的全局变量. BSS是英文Block Started by Symbol的简称.BSS ...
- 自己开发轻量级ORM(二)
上一篇简单的对轻量级ORM开发开了个头.这篇主要聊下ORM框架的设计思路. ORM本质上是对数据库操作的抽象.大体上我将其分为对数据结构的抽象和对执行方法的抽象. 我的ORM设计图: ORM框架需要完 ...
- Linux驱动技术(一) _内存申请
先上基础,下图是Linux的内存映射模型,其中体现了Linux内存映射的几个特点: 每一个进程都有自己的进程空间,进程空间的0-3G是用户空间,3G-4G是内核空间 每个进程的用户空间不在同一个物理内 ...