原题链接

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. springBoot系列教程08:拦截器(Interceptor)的使用

    拦截器intercprot  和 过滤器 Filter 其实作用类似 在最开始接触java 使用struts2的时候,里面都是filter 后来springmvc时就用interceptor 没太在意 ...

  2. java 信号量Semaphore

    Semaphore 信号量主要用于约束多个线程可同时获取的物理上的或者逻辑上的资源数.比如用在各种池的设计中. 信号量用于管理这些资源的一个虚拟的管理凭据.线程在获取一个资源时,首先要获取一个资源的许 ...

  3. Mysql的硬件优化和配置优化

    mysql数据库的优化,算是一个老生常谈的问题了,网上也有很多关于各方面性能优化的例子,今天我们要谈的是MySQL硬件优化和系统参数的优化-即优化my.cnf文件 MySQL的优化我分为两个部分,一是 ...

  4. 三:Redis连接池、JedisPool详解、Redisi分布式

    单机模式: package com.ljq.utils; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; ...

  5. Linux中ls对文件进行按大小排序和按时间排序,设置ls时间格式

    1 按文件大小排序 使用 ll -S | grep '^[^d]' // 格式化文件大小形式 ll -Sh | grep '^[^d]' 2 按文件修改时间排序显示 使用 ll -rt 3 设置ls ...

  6. promise入门demo

    <!DOCTYPE html> <html> <head> <title></title> </head> <body&g ...

  7. 豹哥嵌入式讲堂:ARM开发之文件详解(3)- project文件

    大家好,我是豹哥,猎豹的豹,犀利哥的哥.今天豹哥给大家讲的是嵌入式开发里的project文件. 前面两节课里,豹哥分别给大家介绍了嵌入式开发中的两种典型input文件:source文件.linker文 ...

  8. Python函数返回不定数量的值

    Python的函数是可以return多个值的,但其本质上还是返回单个值,只是利用了tuple的自动打包,将多个值打包成单个tuple返回. 使用代码验证: def func_a(): return 1 ...

  9. 基于SpringBoot开发一个Restful服务,实现增删改查功能

    前言 在去年的时候,在各种渠道中略微的了解了SpringBoot,在开发web项目的时候是如何的方便.快捷.但是当时并没有认真的去学习下,毕竟感觉自己在Struts和SpringMVC都用得不太熟练. ...

  10. JAVA中的设计模式三(策略模式)

    问题: 如何让算法和对象分开来,使得算法可以独立于使用它的客户而变化?   方案: 把一个类中经常改变或者将来可能改变的部分提取出来,作为一个接口,然后在类中包含这个对象的实例,这样类的实例在运行时就 ...