LeetCode 题解工作台

数组中两个数的最大异或值

给你一个整数数组 nums ,返回 nums[i] XOR nums[j] 的最大运算结果,其中 0 ≤ i ≤ j 。 示例 1: 输入: nums = [3,10,5,25,2,8] 输出: 28 解释: 最大运算结果是 5 XOR 25 = 28. 示例 2: 输入: nums = [14,7…

category

4

题型

code_blocks

5

代码语言

hub

3

相关题

当前训练重点

中等 · 数组·哈希·扫描

bolt

答案摘要

题目是求两个元素的异或最大值,可以从最高位开始考虑。 我们把数组中的每个元素 看作一个 位的 串,按二进制从高位到低位的顺序,插入前缀树(最低位为叶子节点)。

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 数组·哈希·扫描 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

给你一个整数数组 nums ,返回 nums[i] XOR nums[j] 的最大运算结果,其中 0 ≤ i ≤ j < n

 

示例 1:

输入:nums = [3,10,5,25,2,8]
输出:28
解释:最大运算结果是 5 XOR 25 = 28.

示例 2:

输入:nums = [14,70,53,83,49,91,36,80,92,51,66,70]
输出:127

 

提示:

  • 1 <= nums.length <= 2 * 105
  • 0 <= nums[i] <= 231 - 1
lightbulb

解题思路

方法一:前缀树

题目是求两个元素的异或最大值,可以从最高位开始考虑。

我们把数组中的每个元素 xx 看作一个 3232 位的 0101 串,按二进制从高位到低位的顺序,插入前缀树(最低位为叶子节点)。

搜索 xx 时,尽量走相反的 0101 字符指针的策略,因为异或运算的法则是相同得 00,不同得 11,所以我们尽可能往与 xx 当前位相反的字符方向走,才能得到能和 xx 产生最大异或值的结果。

时间复杂度 O(n×logM)O(n \times \log M),空间复杂度 O(n×logM)O(n \times \log M),其中 nn 是数组 numsnums 的长度,而 MM 是数组中元素的最大值。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class Trie:
    __slots__ = ("children",)

    def __init__(self):
        self.children: List[Trie | None] = [None, None]

    def insert(self, x: int):
        node = self
        for i in range(30, -1, -1):
            v = x >> i & 1
            if node.children[v] is None:
                node.children[v] = Trie()
            node = node.children[v]

    def search(self, x: int) -> int:
        node = self
        ans = 0
        for i in range(30, -1, -1):
            v = x >> i & 1
            if node.children[v ^ 1]:
                ans |= 1 << i
                node = node.children[v ^ 1]
            else:
                node = node.children[v]
        return ans


class Solution:
    def findMaximumXOR(self, nums: List[int]) -> int:
        trie = Trie()
        for x in nums:
            trie.insert(x)
        return max(trie.search(x) for x in nums)
speed

复杂度分析

指标
时间Depends on the final approach
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    Candidate can demonstrate knowledge of bitwise operations and their applications in optimization problems.

  • question_mark

    Candidate shows ability to use a trie structure for efficient lookups and optimizations.

  • question_mark

    Candidate understands the trade-offs between different approaches in terms of time and space complexity.

warning

常见陷阱

外企场景
  • error

    Not considering the importance of bit-level operations, which can lead to inefficient brute force solutions.

  • error

    Failing to optimize the space complexity when using a hash set or trie to store intermediate results.

  • error

    Overlooking edge cases, such as arrays with only one element or arrays containing only the same number.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Solving the problem without using a trie, relying solely on hash-based solutions.

  • arrow_right_alt

    Modifying the problem to consider an array of very large numbers, affecting time complexity.

  • arrow_right_alt

    Finding the maximum XOR value for subarrays instead of the entire array.

help

常见问题

外企场景

数组中两个数的最大异或值题解:数组·哈希·扫描 | LeetCode #421 中等