当前位置:网站首页>leetcode:421. The maximum XOR value of two numbers in the array
leetcode:421. The maximum XOR value of two numbers in the array
2022-07-04 17:08:00 【Oceanstar's study notes】
Title source
Title Description
class Solution {
public:
int findMaximumXOR(vector<int>& nums) {
}
};
title
because 1<=nums.length < 2 ∗ 1 0 4 2 * 10^4 2∗104, Because the number of instructions is the largest 1 0 8 10^8 108
- If the time complexity of the algorithm is O(N^2), Then the number of instructions
4 * 10^8
, Then it will time out - That is, you must use O(N) perhaps O(N*logN) The algorithm of
What shall I do? ? You can use the prefix number method , See the specific analysis Algorithm : The largest subarray XOR and , The only difference between them is that XOR and are added to the prefix tree , Here we add the array directly to the structure
#include <memory>
struct TrieNode {
std::vector<std::shared_ptr<TrieNode>> next;
explicit TrieNode() : next(2, NULL){
}
};
class NumTrie{
std::shared_ptr<TrieNode> head = std::make_shared<TrieNode>();
public:
void add(int num){
auto curr = head;
for (int i = 31; i >= 0; --i) {
int path = (num >> i) & 1;
curr->next[path] = curr->next[path] == nullptr ? std::make_shared<TrieNode>() : curr->next[path];
curr = curr->next[path];
}
}
int maxXor(int num){
int ans = 0;
auto curr = head;
for (int i = 31; i >= 0; --i){
int path = (num >> i) & 1;
int best = i == 31 ? path : path ^ 1;
best = curr->next[best] == nullptr ? best ^ 1 : best;
curr = curr->next[best];
ans |= ((path ^ best) << i);
}
return ans;
}
};
class Solution {
public:
int findMaximumXOR(vector<int>& nums) {
if(nums.empty()){
return 0;
}
// 0 ≤ i ≤ j < n
int max = INT32_MIN;
NumTrie numTrie;
for (int num : nums) {
numTrie.add(num);
max = std::max(max, numTrie.maxXor(num));
}
return max;
}
};
边栏推荐
- Chow Tai Fook fulfills the "centenary commitment" and sincerely serves to promote green environmental protection
- Market trend report, technical innovation and market forecast of tetrabromophthalate (pht4 diol) in China
- Readis configuration and optimization of NoSQL (final chapter)
- "Cannot initialize Photoshop because the temporary storage disk is full" graphic solution
- 基于check-point机制的任务状态回滚和数据分块任务
- Is it safe to open an account online
- Transformer中position encoding实践
- Maximum subarray and matrix multiplication
- ECCV 2022 released: 1629 papers were selected, and the employment rate was less than 20%
- 线性时间排序
猜你喜欢
World Environment Day | Chow Tai Fook serves wholeheartedly to promote carbon reduction and environmental protection
Which domestic cloud management platform manufacturer is good in 2022? Why?
7 RSA密码体制
2022年国内云管平台厂商哪家好?为什么?
DC-2靶场搭建及渗透实战详细过程(DC靶场系列)
整理混乱的头文件,我用include what you use
Position encoding practice in transformer
The test experience "tortured" by the PMP test is worth your review
一图看懂ThreadLocal
对人胜率84%,DeepMind AI首次在西洋陆军棋中达到人类专家水平
随机推荐
Research Report on plastic recycling machine industry - market status analysis and development prospect forecast
Go language loop statement (under Lesson 10)
. Net applications consider x64 generation
Redis 的内存淘汰策略和过期删除策略的区别
~89 deformation translation
~88 running people practice
GO开发:如何利用Go单例模式保障流媒体高并发的安全性?
科普达人丨一文看懂阿里云的秘密武器“神龙架构”
[Acwing] 58周赛 4489. 最长子序列
ECCV 2022放榜了:1629篇论文中选,录用率不到20%
How to implement a delay queue?
DC-2靶场搭建及渗透实战详细过程(DC靶场系列)
How to decrypt worksheet protection password in Excel file
Understand asp Net core - Authentication Based on jwtbearer
Is it safe for CITIC Securities to open an account online? Is the account opening fee charged
C implementation defines a set of intermediate SQL statements that can be executed across libraries
overflow:auto与felx结合的用法
World Environment Day | Chow Tai Fook serves wholeheartedly to promote carbon reduction and environmental protection
表单传递时,如何隐式将值传过去
照明行业S2B2B解决方案:高效赋能产业供应链,提升企业经济效益