原题链接

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. bzoj 2427: [HAOI2010]软件安装

    Description 现在我们的手头有N个软件,对于一个软件i,它要占用Wi的磁盘空间,它的价值为Vi.我们希望从中选择一些软件安装到一台磁盘容量为M计算机上,使得这些软件的价值尽可能大(即Vi的和 ...

  2. grep 与 find 简单命令

    在使用linux的时候,经常会用到查找文件或者查找文本,下面介绍两个命令. grep 使用方法: // 在当前目录下递归查找class字符串 grep "string" -r . ...

  3. > library('ggplot2') Error in loadNamespace(i, c(lib.loc, .libPaths()), versionCheck = vI[[i]]) : 不存在叫‘colorspace’这个名字的程辑包

    > library('ggplot2')Error in loadNamespace(i, c(lib.loc, .libPaths()), versionCheck = vI[[i]]) : ...

  4. 一、JavaSE语言概述

    1.软件:系统软件 VS 应用软件 2.人与计算交互:使用计算机语言.图形化界面VS命令行. 3.语言的分类:第一代:机器语言 第二代:汇编语言 第三代语言:高级语言(面向过程-面向对象) 4.jav ...

  5. NPOI 2.0 教程

    NPOI2.0帮助官方地址 目录 1. 前言 1.1 NPOI 2.0与NPOI 1.x的区别 1.2 NPOI 2.0模块简介 1.3 自动识别并打开Excel 2003和Excel 2007文件 ...

  6. 该帐户当前被锁定,所以用户sa登录失败。系统管理员无法将该帐户解锁

    运行程序时抛出异常: 基础提供程序在 Open 上失败,详细信息:该帐户当前被锁定,所以用户sa登录失败.系统管理员无法将该帐户解锁. 1.考虑连接字符串是否正常 登录数据库,发现登录报同样的错误 = ...

  7. java基础,集合,HashMap,源码解析

    最怕,你以为你懂咯,其实你还不懂: 见贤思齐,看看那些我们习以为常的集合,通过相关定义.源码,思考分析,加深对其的理解,提高编码能力,能做一个略懂的程序员: 做几个我们常用的集合类.开篇HashMap ...

  8. 轻量级弹出框 lightbox

    1. 引入  lightbox.css 和 lightbox.js 2.检查 CSS 并确定调用的 prev.gif 和 next.gif 文件在正确的位置. 同样要确定调用的 loading.gif ...

  9. 用ildasm和ilasm对.net下的exe程序进行破解初探

    1.对ildasm和ilasm的解释和用法在msdn上有. ildasm:MSIL 反汇编程序是 MSIL 汇编程序 (Ilasm.exe) 的伙伴工具. Ildasm.exe 采用包含 Micros ...

  10. 【转】Mac端包管理工具——Homebrew简介及安装

    Homebrew官网 http://brew.sh/index_zh-cn.html Homebrew是神马 linux系统有个让人蛋疼的通病,软件包依赖,好在当前主流的两大发行版本都自带了解决方案, ...