当前位置:网站首页>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 Sodium Tetraphenylborate (cas+143-66-8) industry
- World Environment Day | Chow Tai Fook serves wholeheartedly to promote carbon reduction and environmental protection
- 力扣今日题-1200. 最小绝对差
- 【云原生】服务网格是什么“格”?
- [acwing] 58 weeks 4489 Longest subsequence
- Solution du système de gestion de la chaîne d'approvisionnement du parc logistique intelligent
- China's plastic processing machinery market trend report, technological innovation and market forecast
- 如何实现一个延时队列 ?
- 金额计算用 BigDecimal 就万无一失了?看看这五个坑吧~~
- overflow:auto与felx结合的用法
猜你喜欢
Can you really use MySQL explain?

Overflow: the combination of auto and Felx
Why do you say that the maximum single table of MySQL database is 20million? Based on what?

【云原生】服务网格是什么“格”?

Learn more about the basic situation of 2022pmp examination

一图看懂ThreadLocal

The test experience "tortured" by the PMP test is worth your review

智慧物流園區供應鏈管理系統解决方案:數智化供應鏈賦能物流運輸行業供應鏈新模式

聊聊异步编程的 7 种实现方式

Principle and general steps of SQL injection
随机推荐
MVC模式和三层架构
Median and order statistics
周大福践行「百周年承诺」,真诚服务推动绿色环保
【Go ~ 0到1 】 第六天 文件的读写与创建
安信证券网上开户安全吗 开户收费吗
智慧物流园区供应链管理系统解决方案:数智化供应链赋能物流运输行业供应链新模式
Congratulations to Mr. Zhang Pengfei, chief data scientist of artefact, for winning the campaign Asia tech MVP 2022
Principle and general steps of SQL injection
Go development: how to use go singleton mode to ensure the security of high concurrency of streaming media?
金额计算用 BigDecimal 就万无一失了?看看这五个坑吧~~
中位数与次序统计量
网页游戏引擎
嵌入式软件架构设计-函数调用
Array filter fliter in JS
Embedded software architecture design - function call
C implementation defines a set of intermediate SQL statements that can be executed across libraries
新的职业已经出现,怎么能够停滞不前 ,人社部公布建筑新职业
Visual Studio 2019 (LocalDB)MSSQLLocalDB SQL Server 2014 数据库版本为852无法打开,此服务器支持782
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
GO开发:如何利用Go单例模式保障流媒体高并发的安全性?