题目

Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation.

Note:

  1. The given integer is guaranteed to fit within the range of a 32-bit signed integer.
  2. 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的更多相关文章

  1. 【leetcode】476. Number Complement

    problem 476. Number Complement solution1: class Solution { public: int findComplement(int num) { //正 ...

  2. LeetCode#476 Number Complement - in Swift

    Given a positive integer, output its complement number. The complement strategy is to flip the bits ...

  3. LeetCode 476. Number Complement

    Given a positive integer, output its complement number. The complement strategy is to flip the bits ...

  4. LeetCode 476. Number Complement (数的补数)

    Given a positive integer, output its complement number. The complement strategy is to flip the bits ...

  5. 【LeetCode】476. Number Complement (java实现)

    原题链接 https://leetcode.com/problems/number-complement/ 原题 Given a positive integer, output its comple ...

  6. LeetCode 476 Number Complement 解题报告

    题目要求 Given a positive integer, output its complement number. The complement strategy is to flip the ...

  7. [LeetCode&Python] Problem 476. Number Complement

    Given a positive integer, output its complement number. The complement strategy is to flip the bits ...

  8. 476. Number Complement 二进制中的相反对应数

    [抄题]: Given a positive integer, output its complement number. The complement strategy is to flip the ...

  9. LeetCode: 476 Number Complement(easy)

    题目: Given a positive integer, output its complement number. The complement strategy is to flip the b ...

随机推荐

  1. 用Redis作为Mysql数据库的缓存

    看到一篇不错的博文,记录下: http://blog.csdn.net/qtyl1988/article/details/39553339 http://blog.csdn.net/qtyl1988/ ...

  2. Bloom Filter的基本原理和变种

    学习一个东西首先要知道这个东西是什么,可以做什么,接着再了解这个东西有什么好处和优势,然后再学习他的工作原理.下面我们分别从这三点简单介绍一下bloom filter,以及和他的变种. What:在允 ...

  3. HMX-Server C++ 分步式服务器大版本更新了(有源码)

    原文地址:http://www.cnblogs.com/hellohuang/p/6294763.html # HMX-ServerHMX-Server分步式服务器框架,主要分为网关.登录.世界.场景 ...

  4. 在Node.js中使用RabbitMQ系列二 任务队列

    在上一篇文章在Node.js中使用RabbitMQ系列一 Hello world我有使用一个任务队列,不过当时的场景是将消息发送给一个消费者,本篇文章我将讨论有多个消费者的场景. 其实,任务队列最核心 ...

  5. 【鸡年大吉】,不知道写点啥,放个demo(小球碰撞)吧,有兴趣的看看

    最初的想法是仿写win7的泡泡屏保效果,但是对于小球的斜碰问题一直没搞明白(如果你会这个,欢迎留言或者做个demo),所以只是简单处理了碰撞后的速度,有时候会看起来很搞笑~~~funny guy 话不 ...

  6. java_db常见错误总结

    1.java.sql.SQLSyntaxErrorException: ORA-00911: 无效字符 在拼写seq时是否存在存在特殊字符,如:常见语句后面添加了;2.ORA-01722:无效数字解决 ...

  7. 用 Lua 实现一个微型虚拟机-基本篇

    用 Lua 实现一个微型虚拟机-基本篇 目录 介绍 机器指令模拟 最终核心代码 虚拟机内部状态可视化 完整项目代码 后续计划 参考 介绍 在网上看到一篇文章 使用 C 语言实现一个虚拟机, 这里是他的 ...

  8. PHP 数组处理

    一:PHP 定义数组: PHP 代码  不能再 空的位置 打字  会报错 定义数组  方式1 $cars=array("Volvo","BMW","T ...

  9. UITableView、UICollectionView行高/尺寸自适应

    UITableView 我们都知道UITableView从iOS 8开始实现行高的自适应相对比较简单,首先必须设置estimatedRowHeight给出预估高度,设置rowHeight为UITabl ...

  10. border-radius属性值参数详解

    border-radius是CSS3设置圆角的一个属性,其属性值得单位可以使用:em.px.百分比等等.但是,border-radius属性值得参数形式有好多种,那么具体都代表什么意思呢?下面以实际例 ...