原题链接

https://leetcode.com/problems/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.

题目要求

题目要求为:给定一个非负整数,求出其complement number。所谓complement number,指对整数二进制最高位为1的位之后的所有位置取反,如5的二进制表示为00……00101,起最高位为1的位置是3,因此只对3之后的所有位置取反,得到00*00010,最后得出complement number为2。

解法

解法一:先求出最高位为1的位数,然后计算出掩码,再将原数据取反后和掩码求与,即得出最后结果。

public int findComplement(int num) {
int valid = 0; // 最高位为1的位数
int tmp = num;
while(tmp > 0) {
tmp /= 2;
valid++;
} return ~num & ((1 << valid) - 1);
}

其中,(1 << valid) - 1是二进制操作中一种常用的求掩码的方式,如valid为3,那么1<<valid二进制为1000,再减去1就得到了0000111。

Top解法:Top解法与解法一完全一致,只是使用了Java库实现的求出最高位为1的库函数而已。当然,代码更加简洁。

public int findComplement(int num) {
return ~num & ((Integer.highestOneBit(num) << 1) - 1);
}

测试用例:

public static void main(String[] args) {
Solution s = new Solution();
assert(s.findComplement(5) == 2);
assert(s.findComplement(1) == 0);
assert(s.findComplement(0) == 0);
}

【LeetCode】476. Number Complement (java实现)的更多相关文章

  1. LeetCode#476 Number Complement - in Swift

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

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

    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 ...

  5. LeetCode: 476 Number Complement(easy)

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

  6. 【leetcode】476. Number Complement

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

  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 bi ...

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

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

随机推荐

  1. jest for elasticsearch

    *elasticsearch(后面简称es) 背景: 目前项目应用中对es的操作用的是http(自己封装)的一套方法:有些数据处理起来还是需要定制开发处理,不是很方便.正好需要对本项目重新进行改造,于 ...

  2. MySQL: Integer & String types

    Type Storage(bytes) Minimum Value Maximum Value TINYINT 1 -128/0 127/255 SMALLINT 2 -23768/0 23767/6 ...

  3. Xampp配置本地域名及常见错误解决

    本地域名配置 1.计算机-->C盘-->Windows-->System32-->drivers-->etc-->hosts 127.0.0.1       loc ...

  4. 准确率(Accuracy), 精确率(Precision), 召回率(Recall)和F1-Measure(对于二分类问题)

    首先我们可以计算准确率(accuracy),其定义是: 对于给定的测试数据集,分类器正确分类的样本数与总样本数之比.也就是损失函数是0-1损失时测试数据集上的准确率. 下面在介绍时使用一下例子: 一个 ...

  5. 遍历文件 创建XML对象 方法 python解析XML文件 提取坐标计存入文件

    XML文件??? xml即可扩展标记语言,它可以用来标记数据.定义数据类型,是一种允许用户对自己的标记语言进行定义的源语言. 里面的标签都是可以随心所欲的按照他的命名规则来定义的,文件名为roi.xm ...

  6. 命令行执行Django脚本的方法

    update.py import os import sys import django sys.path.append(r'C:\Users\Administrator\PycharmProject ...

  7. Celery(四)定时任务

    要定时或者周期性的执行任务,可以使用linux的crontab.Celery也提供了类似的Periodic Tasks功能. Celery beat Celery使用celery beat作为任务调度 ...

  8. voip技术研究

    voip:是一种通过ip现实电话通信的技术统称 sip:voip现在一般都采用sip协议 参考资料: android sip学习 问题: SipManager.newInstance(this)为nu ...

  9. 模拟uClinux系统调用

    这篇文章原来放在CU上的,现在挪过来了.CU上设置不可见了. 1.  目标 这里主要是实验一下uclinux的系统调用. 2.   环境 OS                :vmware + red ...

  10. 【转】Install libimobiledevice on Mac OSX

    About the App App name: libimobiledevice App description: Library to communicate with iOS devices na ...