当前位置:网站首页>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;
}
};

边栏推荐
猜你喜欢

太方便了,钉钉上就可完成代码发布审批啦!

照明行业S2B2B解决方案:高效赋能产业供应链,提升企业经济效益

《吐血整理》保姆级系列教程-玩转Fiddler抓包教程(2)-初识Fiddler让你理性认识一下

Object. Usage of keys()

新的职业已经出现,怎么能够停滞不前 ,人社部公布建筑新职业

一图看懂ThreadLocal

overflow:auto与felx结合的用法

Kunming Third Ring Road Closure project will pass through these places. Is there one near your home?

电子元器件B2B商城系统开发:赋能企业构建进销存标准化流程实例

Object.keys()的用法
随机推荐
Unity interview questions (continuously updated)
How to implement a delay queue?
Four point probe Industry Research Report - market status analysis and development prospect prediction
Pytorch deep learning quick start tutorial
The winning rate against people is 84%, and deepmind AI has reached the level of human experts in army chess for the first time
线性时间排序
Visual studio 2019 (localdb) mssqllocaldb SQL Server 2014 database version is 852 and cannot be opened. This server supports 782
Research Report on market supply and demand and strategy of China's well completion equipment industry
Configuration instance of Oracle listener server and client
L1-072 scratch lottery
Use and principle of thread pool
Solution of commercial supply chain coordination system in the mineral industry: build a digital intelligent supply chain platform to ensure the safe supply of mineral resources
基于check-point机制的任务状态回滚和数据分块任务
时序图数据建模与产业链分析
overflow:auto与felx结合的用法
Research Report on market supply and demand and strategy of China's plastics and polymer industry
Is it safe for CITIC Securities to open an account online? Is the account opening fee charged
中银证券网上开户安全吗?
Start by counting
电子元器件B2B商城系统开发:赋能企业构建进销存标准化流程实例