当前位置:网站首页>leetcode:421. 数组中两个数的最大异或值
leetcode:421. 数组中两个数的最大异或值
2022-07-04 15:12:00 【OceanStar的学习笔记】
题目来源
题目描述
class Solution {
public:
int findMaximumXOR(vector<int>& nums) {
}
};
题目解析
因为1<=nums.length < 2 ∗ 1 0 4 2 * 10^4 2∗104,又因为指令数最大 1 0 8 10^8 108
- 如果算法的时间复杂度为O(N^2),那么指令数
4 * 10^8
,那么会超时 - 也就是必须用O(N)或者O(N*logN)的算法
怎么办呢?可以用前缀数的方法,具体解析看算法:最大子数组异或和,它们之间的唯一区别是之前是将异或和加入前缀树,这里直接把数组加入到结构里
#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;
}
};
边栏推荐
- Cypher task design and task locking mechanism of isomorphic and heterogeneous graphs
- Market trend report, technical innovation and market forecast of electrochromic glass and devices in China and Indonesia
- Sql实现Split
- Accounting regulations and professional ethics [7]
- 基于wifi控制的51单片机温度报警器
- PyTorch深度学习快速入门教程
- 2022PMP考试基本情况详情了解
- 智慧物流园区供应链管理系统解决方案:数智化供应链赋能物流运输行业供应链新模式
- Detailed process of DC-2 range construction and penetration practice (DC range Series)
- 安信证券排名 网上开户安全吗
猜你喜欢
世界环境日 | 周大福用心服务推动减碳环保
照明行业S2B2B解决方案:高效赋能产业供应链,提升企业经济效益
World Environment Day | Chow Tai Fook serves wholeheartedly to promote carbon reduction and environmental protection
科普达人丨一文看懂阿里云的秘密武器“神龙架构”
NoSQL之readis配置与优化(终章)
Object. Usage of keys()
电子元器件B2B商城系统开发:赋能企业构建进销存标准化流程实例
Capvision Rongying's prospectus in Hong Kong was "invalid": it was strictly questioned by the CSRC and required supplementary disclosure
Statistical learning: logistic regression and cross entropy loss (pytoch Implementation)
How to decrypt worksheet protection password in Excel file
随机推荐
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
Hair growth shampoo industry Research Report - market status analysis and development prospect forecast
How to implicitly pass values when transferring forms
World Environment Day | Chow Tai Fook serves wholeheartedly to promote carbon reduction and environmental protection
时序图数据建模与产业链分析
MFC implementation of ACM basic questions encoded by the number of characters
Vscode setting outline shortcut keys to improve efficiency
Visual studio 2019 (localdb) mssqllocaldb SQL Server 2014 database version is 852 and cannot be opened. This server supports 782
The vscode waveform curve prompts that the header file cannot be found (an error is reported if the header file exists)
Understand ThreadLocal in one picture
What is torch NN?
Cut! 39 year old Ali P9, saved 150million
手里10万元存款买什么理财产品收益最高?
Object.keys()的用法
Opencv learning -- geometric transformation of image processing
Understand asp Net core - Authentication Based on jwtbearer
Statistical learning: logistic regression and cross entropy loss (pytoch Implementation)
~89 deformation translation
2021 Google vulnerability reward program review