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

边栏推荐
- Opencv learning -- arithmetic operation of image of basic operation
- Research Report on market supply and demand and strategy of surgical stapler industry in China
- GO开发:如何利用Go单例模式保障流媒体高并发的安全性?
- System.currentTimeMillis() 和 System.nanoTime() 哪个更快?别用错了!
- Market trend report, technical innovation and market forecast of electrochromic glass and devices in China and Indonesia
- 祝贺Artefact首席数据科学家张鹏飞先生荣获 Campaign Asia Tech MVP 2022
- 智慧物流园区供应链管理系统解决方案:数智化供应链赋能物流运输行业供应链新模式
- Market trend report, technical innovation and market forecast of taillight components in China
- What is torch NN?
- Detailed process of DC-2 range construction and penetration practice (DC range Series)
猜你喜欢

~89 deformation translation

Object.keys()的用法

I let the database lock the table! Almost fired!

DIY a low-cost multi-functional dot matrix clock!

智慧物流园区供应链管理系统解决方案:数智化供应链赋能物流运输行业供应链新模式

Understand asp Net core - Authentication Based on jwtbearer

如何实现一个延时队列 ?

建筑建材行业经销商协同系统解决方案:赋能企业构建核心竞争力

周大福践行「百周年承诺」,真诚服务推动绿色环保

"Cannot initialize Photoshop because the temporary storage disk is full" graphic solution
随机推荐
ONgDB图数据库与Spark的集成
矿产行业商业供应链协同系统解决方案:构建数智化供应链平台,保障矿产资源安全供应
GO开发:如何利用Go单例模式保障流媒体高并发的安全性?
Accounting regulations and professional ethics [9]
go-micro教程 — 第二章 go-micro v3 使用Gin、Etcd
Accounting regulations and professional ethics [8]
Application of clock wheel in RPC
Can you really use MySQL explain?
Accounting regulations and professional ethics [10]
Accounting regulations and professional ethics [7]
Research Report on market supply and demand and strategy of China's four sided flat bag industry
Accounting regulations and professional ethics [11]
多年锤炼,迈向Kata 3.0 !走进开箱即用的安全容器体验之旅| 龙蜥技术
Practice: fabric user certificate revocation operation process
[Chongqing Guangdong education] National Open University spring 2019 1248 public sector human resource management reference questions
Overflow: the combination of auto and Felx
[North Asia data recovery] a database data recovery case where the disk on which the database is located is unrecognized due to the RAID disk failure of HP DL380 server
线程池的使用和原理
智慧物流园区供应链管理系统解决方案:数智化供应链赋能物流运输行业供应链新模式
Sequence diagram data modeling and industrial chain analysis