Skip to content

Commit 4271b01

Browse files
committed
2019-11-30 421. Maximum XOR of Two Numbers in an Array Solution
1 parent e087849 commit 4271b01

1 file changed

Lines changed: 87 additions & 0 deletions

File tree

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# LeetCode 421. Maximum XOR of Two Numbers in an Array
2+
3+
## Description
4+
5+
Given a non-empty array of numbers, a0, a1, a2, … , an-1, where 0 ≤ ai < 231.
6+
7+
Find the maximum result of ai XOR aj, where 0 ≤ i, j < n.
8+
9+
Could you do this in O(n) runtime?
10+
11+
Example:
12+
13+
```py
14+
Input: [3, 10, 5, 25, 2, 8]
15+
16+
Output: 28
17+
18+
Explanation: The maximum result is 5 ^ 25 = 28.
19+
```
20+
## 描述
21+
22+
给定一个非空数组,数组中元素为 a0, a1, a2, … , an-1,其中 0 ≤ ai < 231 。
23+
24+
找到 ai 和aj 最大的异或 (XOR) 运算结果,其中0 ≤ i,  j < n 。
25+
26+
你能在O(n)的时间解决这个问题吗?
27+
28+
示例:
29+
30+
```py
31+
输入: [3, 10, 5, 25, 2, 8]
32+
33+
输出: 28
34+
35+
解释: 最大的结果是 5 ^ 25 = 28.
36+
```
37+
38+
来源:力扣(LeetCode)
39+
链接:https://leetcode-cn.com/problems/maximum-xor-of-two-numbers-in-an-array
40+
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
41+
### 思路
42+
43+
* 抑或运算有一个性质,如果 a ^ b = c 那么 a ^ c = b。
44+
* 题目给定数据的输入范围是 32 int 类型,那么其抑或得到的结果也是 32 位。
45+
* 基本思路是,我们考察这个最大值的每一位有没有可能是 1。
46+
* 从最高位置开始考察,如果该位置可以为 1,则置为 1,否则置为 0.
47+
* 设最终结果 res = 0,步骤如下:
48+
49+
1. 我们取每个数(每个数都有 32 位,不足用 0 占位)的前一位,将这些数添加到一个 set 中,置 tmp_res = res,将 tmp_res 向右移动一位,用 tmp_res 依次抑或每一个值,得到结果 tmp,如果 tmp 也在 set 中,说明,存在两个数抑或的结果为 tmp_res,更新 res = tmp_res。
50+
2. 取每个数的前 2 位,添加到 set(初始为空,不是上一次结果的 set ) 中。置 tmp_res = res,将 tmp_res 向右移动一位。用 tmp_res 依次抑或每一个值得到结果 tmp,如果所有的 tmp 都不在 set 中,说明不存在两个数抑或的结果为 tmp_res,res 不更新。
51+
3. 取每个数的前 3 位,添加到 set 中。tmp_res 置为 res,并向右移动一位。依次与 set 中每个值抑或,检查抑或的结果是否也在 set 中,若是,更新 res = tmp_res,否则不更新 res
52+
4. ...
53+
5. 返回 res
54+
55+
```py
56+
# -*- coding: utf-8 -*-
57+
# @Author: 何睿
58+
# @Create Date: 2019-11-30 09:35:38
59+
# @Last Modified by: 何睿
60+
# @Last Modified time: 2019-11-30 10:10:40
61+
62+
from typing import List
63+
64+
65+
class Solution:
66+
def findMaximumXOR(self, nums: List[int]) -> int:
67+
res, mask = 0, 0
68+
for i in range(31, -1, -1):
69+
# mask 值的变化是:
70+
# mask = 0 | 10000000000000000000000000000000 = 10000000000000000000000000000000
71+
# mask = 10000000000000000000000000000000 | 01000000000000000000000000000000 = 11000000000000000000000000000000
72+
# mask = 11000000000000000000000000000000 | 00100000000000000000000000000000 = 11100000000000000000000000000000
73+
mask |= (1 << i) # 用来取每个数的前 1 为,前 2 位 ...
74+
prefix = set(num & mask for num in nums) # 将前 n 位的结果添加到空 set 中
75+
tmp = res | (1 << i) # 考察 res 的下一位是否可以为 1
76+
for s in prefix:
77+
if tmp ^ s in prefix:
78+
res = tmp
79+
break
80+
81+
return res
82+
83+
```
84+
85+
源代码文件在 [这里](https://github.com/ruicore/Algorithm/blob/master/LeetCode/2019-11-30-421-Maximum-XOR-of-Two-Numbers-in-an-Array.py)
86+
©本文是原创文章,欢迎转载,转载需保留 [文章来源](https://ruicore.cn/leetcode-421-maximum-xor-of-two-numbers-in-an-array/) ,作者信息和本声明.
87+
©本文首发于 何睿的博客 ,欢迎转载,转载需保留 [文章来源](https://ruicore.cn/leetcode-421-maximum-xor-of-two-numbers-in-an-array/) ,作者信息和本声明.

0 commit comments

Comments
 (0)