当前位置:网站首页>Sword finger offer II 067. maximum XOR (medium prefix tree bit operation array)
Sword finger offer II 067. maximum XOR (medium prefix tree bit operation array)
2022-07-28 22:25:00 【Calm in the wind and rain】
The finger of the sword Offer II 067. Largest exclusive or
Given an array of integers nums , return nums[i] XOR nums[j] The largest operation result of , among 0 ≤ i ≤ j < n .
Example 1:
Input :nums = [3,10,5,25,2,8]
Output :28
explain : The maximum result is 5 XOR 25 = 28.
Example 2:
Input :nums = [0]
Output :0
Example 3:
Input :nums = [2,4]
Output :6
Example 4:
Input :nums = [8,10,2]
Output :10
Example 5:
Input :nums = [14,70,53,83,49,91,36,80,92,51,66,70]
Output :127
Tips :
1 <= nums.length <= 2 * 104
0 <= nums[i] <= 231 - 1
Advanced : You can O(n) Is it time to solve this problem ?
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/ms70jA
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
analysis
Using prefix tree to realize O(n) Time complexity of , There are at most two nodes in each layer of the prefix tree 0 and 1, The first traversal will be the 32 Bits exist in the prefix tree , In the second traversal, XOR judgment is performed on each number and prefix tree to obtain the maximum XOR value .
Answer key (Java)
class Solution {
static class TrieNode {
TrieNode[] children;
public TrieNode() {
children = new TrieNode[2];
}
}
public int findMaximumXOR(int[] nums) {
TrieNode root = buildTrie(nums);
int ans = 0;
for (int num : nums) {
TrieNode node = root;
int xor = 0;
for (int i = 31; i >= 0; i--) {
int bit = (num >> i) & 1;
if (node.children[1 - bit] != null) {
xor = (xor << 1) + 1;
node = node.children[1 - bit];
} else {
xor = xor << 1;
node = node.children[bit];
}
}
ans = Math.max(xor, ans);
}
return ans;
}
private TrieNode buildTrie(int[] nums) {
TrieNode root = new TrieNode();
for (int num : nums) {
TrieNode node = root;
for (int i = 31; i >= 0; i--) {
int bit = (num >> i) & 1;
if (node.children[bit] == null) {
node.children[bit] = new TrieNode();
}
node = node.children[bit];
}
}
return root;
}
}
边栏推荐
猜你喜欢

Data visualization news, different forms of news reports

Embrace open source guidelines

软考网络工程师

IFLYTEK written examination

hcip实验(14)

Sword finger offer II 052. flatten binary search tree (simple binary search tree DFS)

hcip实验(15)

DHCP和PPPoE协议以及抓包分析
![[LiteratureReview]Object Detection and Mapping with Bounding Box Constraints](/img/37/7cb5fa3a9078a5f5947485147c819d.png)
[LiteratureReview]Object Detection and Mapping with Bounding Box Constraints

Learning notes and summary of C language programming specification
随机推荐
04. Default value of toref
internet的基本服务中文件传输命令是哪个
Typeof principle
Part 8: creating camera classes
HCIP(10)
SQL injection less42 (post stack injection)
Desai wisdom number - line chart (stacking area chart): ranking of deposits of different occupational groups in the proportion of monthly income in 2022
Add DNS server to LAN for domain name resolution
hcip实验(14)
Summary of the use of hash table set and map when leetcode brushes questions
Written examination summary record
AimBetter洞察您的数据库,DPM 和 APM 解决方案
Jianzhi offer II 062. implement prefix tree (medium design dictionary tree prefix tree string)
hcip实验(12)
IFLYTEK written examination
笔试总结记录
Sword finger offer II 057. the difference between the value and the subscript is within the given range (medium array bucket sort sliding window TreeSet)
Which is the file transfer command in the basic services of the Internet
Sword finger offer II 053. Medium order successor in binary search tree (medium binary search tree DFS)
gprs网络指的是什么