当前位置:网站首页>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;
}
};
边栏推荐
- Research Report on market supply and demand and strategy of China's well completion equipment industry
- NoSQL之readis配置与优化(终章)
- [Chongqing Guangdong education] National Open University spring 2019 1248 public sector human resource management reference questions
- Redis 的内存淘汰策略和过期删除策略的区别
- Array filter fliter in JS
- 容器环境minor gc异常频繁分析
- Research Report on market supply and demand and strategy of China's Sodium Tetraphenylborate (cas+143-66-8) industry
- 安信证券手机版下载 网上开户安全吗
- How can programmers improve the speed of code writing?
- APOC自定义函数和过程
猜你喜欢
程序员怎么才能提高代码编写速度?
Understand asp Net core - Authentication Based on jwtbearer
智慧物流园区供应链管理系统解决方案:数智化供应链赋能物流运输行业供应链新模式
世界环境日 | 周大福用心服务推动减碳环保
Detailed process of DC-2 range construction and penetration practice (DC range Series)
Start by counting
整理混乱的头文件,我用include what you use
MVC模式和三层架构
聊聊异步编程的 7 种实现方式
矿产行业商业供应链协同系统解决方案:构建数智化供应链平台,保障矿产资源安全供应
随机推荐
基于wifi控制的51单片机温度报警器
高度剩余法
[Acwing] 58周赛 4490. 染色
【云原生】服务网格是什么“格”?
VMware Tools和open-vm-tools的安装与使用:解决虚拟机不全屏和无法传输文件的问题
Object. Usage of keys()
Principle and general steps of SQL injection
Li Kou today's question -1200 Minimum absolute difference
How can programmers improve the speed of code writing?
[glide] cache implementation - memory and disk cache
建筑建材行业经销商协同系统解决方案:赋能企业构建核心竞争力
Implement graph data construction task based on check point
照明行业S2B2B解决方案:高效赋能产业供应链,提升企业经济效益
Research Report on market supply and demand and strategy of China's Sodium Tetraphenylborate (cas+143-66-8) industry
安信证券手机版下载 网上开户安全吗
Sequence diagram data modeling and industrial chain analysis
Go micro tutorial - Chapter 2 go micro V3 using gin and etcd
安信证券排名 网上开户安全吗
Use and principle of thread pool
Is it safe for Anxin securities to open an account online? Is the account opening fee charged