当前位置:网站首页>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;
}
};
边栏推荐
- [acwing] 58 weeks 4489 Longest subsequence
- egg. JS learning notes
- 散列表
- 安信证券排名 网上开户安全吗
- How to contribute to the source code of ongdb core project
- 最大子数组与矩阵乘法
- System. Currenttimemillis() and system Nanotime (), which is faster? Don't use it wrong!
- Research Report on plastic recycling machine industry - market status analysis and development prospect forecast
- 话里话外:流程图绘制初级:六大常见错误
- 时序图数据建模与产业链分析
猜你喜欢
7 RSA密码体制
MVC模式和三层架构
聊聊异步编程的 7 种实现方式
How to implement a delay queue?
The winning rate against people is 84%, and deepmind AI has reached the level of human experts in army chess for the first time
Redis 的内存淘汰策略和过期删除策略的区别
太方便了,钉钉上就可完成代码发布审批啦!
NoSQL之readis配置与优化(终章)
GO开发:如何利用Go单例模式保障流媒体高并发的安全性?
Object.keys()的用法
随机推荐
线程池的使用和原理
leetcode:421. 数组中两个数的最大异或值
【云原生】服务网格是什么“格”?
c# 实现定义一套中间SQL可以跨库执行的SQL语句
Unity interview questions (continuously updated)
建筑建材行业经销商协同系统解决方案:赋能企业构建核心竞争力
被PMP考试“折磨”出来的考试心得,值得你一览
Go语言循环语句(第10课下)
Go micro tutorial - Chapter 2 go micro V3 using gin and etcd
话里话外:流程图绘制初级:六大常见错误
China's roof ladder market trend report, technological innovation and market forecast
Object. Usage of keys()
中信证券网上开户安全吗 开户收费吗
How to implement a delay queue?
APOC custom functions and procedures
Start by counting
The test experience "tortured" by the PMP test is worth your review
Cypher task design and task locking mechanism of isomorphic and heterogeneous graphs
同构图与异构图CYPHER-TASK设计与TASK锁机制
Understand ThreadLocal in one picture