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

边栏推荐
- Hair and fuzz interceptor Industry Research Report - market status analysis and development prospect forecast
- Accounting regulations and professional ethics [6]
- 表单传递时,如何隐式将值传过去
- GO开发:如何利用Go单例模式保障流媒体高并发的安全性?
- egg. JS learning notes
- 对人胜率84%,DeepMind AI首次在西洋陆军棋中达到人类专家水平
- 世界环境日 | 周大福用心服务推动减碳环保
- ECCV 2022 released: 1629 papers were selected, and the employment rate was less than 20%
- Research Report on market supply and demand and strategy of China's Sodium Tetraphenylborate (cas+143-66-8) industry
- Start by counting
猜你喜欢

Go development: how to use go singleton mode to ensure the security of high concurrency of streaming media?

Position encoding practice in transformer

D3D11_ Chili_ Tutorial (2): draw a triangle
![[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](/img/21/513042008483cf21fc66729ae1d41f.jpg)
[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

Blood spitting finishing nanny level series tutorial - play Fiddler bag grabbing tutorial (2) - first meet fiddler, let you have a rational understanding

NoSQL之readis配置与优化(终章)

Vscode prompt Please install clang or check configuration 'clang executable‘

MFC implementation of ACM basic questions encoded by the number of characters

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

对人胜率84%,DeepMind AI首次在西洋陆军棋中达到人类专家水平
随机推荐
VMware Tools和open-vm-tools的安装与使用:解决虚拟机不全屏和无法传输文件的问题
基于check-point机制的任务状态回滚和数据分块任务
Maximum subarray and matrix multiplication
Statistical learning: logistic regression and cross entropy loss (pytoch Implementation)
Spark 中的 Rebalance 操作以及与Repartition操作的区别
Communication mode based on stm32f1 single chip microcomputer
如何为ONgDB核心项目源码做贡献
go-micro教程 — 第二章 go-micro v3 使用Gin、Etcd
Research Report of exoskeleton robot industry - market status analysis and development prospect prediction
FIREBIRD使用经验总结
嵌入式软件架构设计-函数调用
多年锤炼,迈向Kata 3.0 !走进开箱即用的安全容器体验之旅| 龙蜥技术
安信证券网上开户安全吗 开户收费吗
China Indonesia adhesive market trend report, technological innovation and market forecast
S2b2b solution for lighting industry: efficiently enable the industrial supply chain and improve the economic benefits of enterprises
Can you really use MySQL explain?
从数数开始
js中的数组筛选fliter
China's plastic processing machinery market trend report, technological innovation and market forecast
"Cannot initialize Photoshop because the temporary storage disk is full" graphic solution