当前位置:网站首页>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;
}
};
边栏推荐
- Understand ThreadLocal in one picture
- Start by counting
- World Environment Day | Chow Tai Fook serves wholeheartedly to promote carbon reduction and environmental protection
- How to decrypt worksheet protection password in Excel file
- "Cannot initialize Photoshop because the temporary storage disk is full" graphic solution
- Market trend report, technical innovation and market forecast of China's hair repair therapeutic apparatus
- [Chongqing Guangdong education] National Open University spring 2019 1248 public sector human resource management reference questions
- . Net applications consider x64 generation
- Understand asp Net core - Authentication Based on jwtbearer
- Detailed process of DC-2 range construction and penetration practice (DC range Series)
猜你喜欢
How to decrypt worksheet protection password in Excel file
Overflow: the combination of auto and Felx
~89 deformation translation
D3D11_ Chili_ Tutorial (2): draw a triangle
VMware Tools和open-vm-tools的安装与使用:解决虚拟机不全屏和无法传输文件的问题
Capvision Rongying's prospectus in Hong Kong was "invalid": it was strictly questioned by the CSRC and required supplementary disclosure
Visual Studio 2019 (LocalDB)MSSQLLocalDB SQL Server 2014 数据库版本为852无法打开,此服务器支持782
[North Asia data recovery] a database data recovery case where the partition where the database is located is unrecognized due to the RAID disk failure of HP DL380 server
Principle and general steps of SQL injection
2022PMP考试基本情况详情了解
随机推荐
Communication mode based on stm32f1 single chip microcomputer
Opencv learning -- geometric transformation of image processing
Capvision Rongying's prospectus in Hong Kong was "invalid": it was strictly questioned by the CSRC and required supplementary disclosure
智慧物流园区供应链管理系统解决方案:数智化供应链赋能物流运输行业供应链新模式
Understand asp Net core - Authentication Based on jwtbearer
手里10万元存款买什么理财产品收益最高?
Accounting regulations and professional ethics [11]
Application of clock wheel in RPC
祝贺Artefact首席数据科学家张鹏飞先生荣获 Campaign Asia Tech MVP 2022
Object.keys()的用法
c# 实现定义一套中间SQL可以跨库执行的SQL语句
Daily notes~
Go development: how to use go singleton mode to ensure the security of high concurrency of streaming media?
Detailed process of DC-2 range construction and penetration practice (DC range Series)
I let the database lock the table! Almost fired!
Final consistency of MESI cache in CPU -- why does CPU need cache
C # realizes FFT forward and inverse transformation and frequency domain filtering
[Acwing] 58周赛 4490. 染色
China's plastic processing machinery market trend report, technological innovation and market forecast
Research Report on market supply and demand and strategy of China's four sided flat bag industry