• 作者: 负雪明烛
  • id: fuxuemingzhu
  • 个人博客: http://fuxuemingzhu.cn/
  • 公众号:负雪明烛
  • 本文关键词:Leetcode, 力扣,476, 补数,二进制,Python, C++, Java

题目地址:https://leetcode.com/problems/number-complement/

  • Difficulty: Easy

题目描述

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.

解题方法

今天题目重点是:补数。

补数是对该数的二进制取反。

注意二进制中是没有前导 0 的,也就是说二进制 表示的数字中,第一位必然是 1。

方法一:取反

位运算中有一个运算符 ~ 表示取反,可不可以用它呢?我们实验一下:

>>> Integer.toBinaryString(5)
101
>>> Integer.toBinaryString(~5)
11111111111111111111111111111010

我们看到,虽然 5 的二进制 101 被取反了,但是其前导 0 也被取反。

所以我们不能直接用 ~ 运算符。

下面的思路就是找到数字的二进制中,第一个 1 的位置了。

对于 Java 而言,有库函数可以使用 Integer.highestOneBit(num) ,它的作用是只保留 num 二进制的最高位的 1 的情况下的数字。

>>> Integer.highestOneBit(5)
4

我们想得到和 num 的二进制位置相等的全 1 的数字,可以用 ((Integer.highestOneBit(num)) << 1) - 1

Java 语言的代码如下:

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

复杂度分析:

  • 时间复杂度:

    O

    (

    1

    )

    O(1)

    O(1),和数据规模无关;

  • 空间复杂度:

    O

    (

    1

    )

    O(1)

    O(1),只使用了常数个空间。

方法二:异或

注意到求补数,就是把现有的二进制表示各位进行了 0, 1 互换,很容易想到异或操作。

0 ^ 1 = 1
1 ^ 1 = 0

所以我们应该把原本的数字的每一位都和 1 进行异或计算。

我们需要构建和源数字的二进制位数相等的全 1 数字。求源数字的二进制数字长度可以用方法一,也可以直接获取二进制字符串长度。

Python 代码如下:

class Solution:
def findComplement(self, num):
return num ^ ((1 << (len(bin(num)) - 2)) - 1)


复杂度分析:

  • 时间复杂度:

    O

    (

    1

    )

    O(1)

    O(1),和数据规模无关;

  • 空间复杂度:

    O

    (

    1

    )

    O(1)

    O(1),只使用了常数个空间。

方法三:二进制字符串

另一种常见的思路是先把输入数字 num 转成二进制字符串,将二进制字符串中的 '0''1' 互换,再转成 10 进制数字。

Python 语言的代码如下:

class Solution:
def findComplement(self, num):
bin_num = bin(num)[2:]
bin_ans = map(lambda x: '0' if x == '1' else '1', bin_num)
return int(''.join(bin_ans), 2)

复杂度分析:

  • 时间复杂度:

    O

    (

    1

    )

    O(1)

    O(1),和数据规模无关;

  • 空间复杂度:

    O

    (

    1

    )

    O(1)

    O(1),只使用了常数个空间。

总结

  1. 这个题的重点是获取二进制数字的长度。
  2. 除了上面的方法外,还可以逐位遍历,找到其二进制的第一个 1。

日期

2017 年 1 月 15 日
2018 年 7 月 4 日
2018 年 11 月 6 日 —— 腰酸背痛要废了
2021 年 10 月 18 日

【LeetCode】476. 数字的补数 Number Complement的更多相关文章

  1. Java实现 LeetCode 476 数字的补数

    476. 数字的补数 给定一个正整数,输出它的补数.补数是对该数的二进制表示取反. 示例 1: 输入: 5 输出: 2 解释: 5 的二进制表示为 101(没有前导零位),其补数为 010.所以你需要 ...

  2. Leetcode 476.数字的补数

    数字的补数 给定一个正整数,输出它的补数.补数是对该数的二进制表示取反. 注意: 给定的整数保证在32位带符号整数的范围内. 你可以假定二进制数不包含前导零位. 示例 1: 输入: 5 输出: 2 解 ...

  3. [Swift]LeetCode476. 数字的补数 | Number Complement

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

  4. 力扣(LeetCode)476. 数字的补数

    给定一个正整数,输出它的补数.补数是对该数的二进制表示取反. 注意: 给定的整数保证在32位带符号整数的范围内. 你可以假定二进制数不包含前导零位. 示例 1: 输入: 5 输出: 2 解释: 5的二 ...

  5. 小练习:补数 (Number Complement)

    1.eamples Input: Output: Explanation: The binary representation of (no leading zero bits), and its c ...

  6. 【leetcode】476. Number Complement

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

  7. LeetCode#476 Number Complement - in Swift

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

  8. LeetCode——Number Complement

    LeetCode--Number Complement Question Given a positive integer, output its complement number. The com ...

  9. C#LeetCode刷题之#136-只出现一次的数字(Single Number)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4046 访问. 给定一个非空整数数组,除了某个元素只出现一次以外, ...

随机推荐

  1. fluidity详解

    fluidity详解 1.fluidity编译过程 1.1.femtools库调用方法 编译fluidity/femtools目录下所有文件,打包为libfemtools.a静态库文件: 通过-lfe ...

  2. shell 小数的比较大小问题

    经过实验,if 语句中的数值判断是不可以比较小数大小的:-gt -ne 2. 直接用awk awk -v  num1=6.6 -v num2=5.5  'BEGIN{print(num1>num ...

  3. Python中pymysql基本使用

    Python中pymysql模块通过获取mysql数据库命令行游标执行数据库命令来进行数据库操作 优点:操作数据库语句所见即所得,执行了什么数据库语句都很清楚 缺点:操作繁琐,代码量多 1. pymy ...

  4. 【学相伴】Nginx最新教程通俗易懂-狂神说

    Nginx - 学相伴 分享人:秦疆(遇见狂神说) 公司产品出现瓶颈? 我们公司项目刚刚上线的时候,并发量小,用户使用的少,所以在低并发的情况下,一个jar包启动应用就够了,然后内部tomcat返回内 ...

  5. 使用 Skywalking 对 Kubernetes(K8s)中的微服务进行监控

    1. 概述 老话说的好:任何成功都不是轻易得来的,是不断地坚持与面对的结果. 言归正传,之前我们聊了 SpringCloud 开发的微服务是如何部署在  Kubernetes(K8s)集群中的,今天我 ...

  6. mysql-centos8下安装

    参考文章 1.下载安装包 客服端与服务端 依赖包 2.linux下检查是否安装 rpm -qa | grep -i mysql 安装过会显示软件名称,没安装过就是空的 3.安装包传到虚拟机 先需要把安 ...

  7. A Child's History of England.1

    A Child's History of England, by Charles Dickens (狄更斯) CHAPTER I ANCIENT ENGLAND AND THE ROMANS If y ...

  8. 修改linux文件权限命令:chmod 转载至 Avril 的随笔

    Linux系统中的每个文件和目录都有访问许可权限,用它来确定谁可以通过何种方式对文件和目录进行访问和操作. 文件或目录的访问权限分为只读,只写和可执行三种.以文件为例,只读权限表示只允许读其内容,而禁 ...

  9. docker之镜像制作

    #:下载镜像并初始化系统 root@ubuntu:~# docker pull centos #:创建目录 root@ubuntu:/opt# mkdir dockerfile/{web/{nginx ...

  10. C++ default constructor | Built-in types

    Predict the output of following program? 1 #include <iostream> 2 using namespace std; 3 4 int ...