当前位置:网站首页>421. maximum XOR value of two numbers in the array
421. maximum XOR value of two numbers in the array
2022-06-11 07:07:00 【Not coriander】
Give you an array of integers nums , return nums[i] XOR nums[j] The largest operation result of , among 0 ≤ i ≤ j < n .
Advanced : You can O(n) Is it time to solve this problem ?
Example 1:
Input :nums = [3,10,5,25,2,8]
Output :28
explain : The maximum result is 5 XOR 25 = 28.
Example 2:
Input :nums = [0]
Output :0
Example 3:
Input :nums = [2,4]
Output :6
Example 4:
Input :nums = [8,10,2]
Output :10
Example 5:
Input :nums = [14,70,53,83,49,91,36,80,92,51,66,70]
Output :127
Tips :
1 <= nums.length <= 2 * 104
0 <= nums[i] <= 231 - 1
2. Prefix
struct Trie {
// Left subtree pointing representation 0 Child nodes of
Trie* left = NULL;
// The right subtree points to 1 Child nodes of
Trie* right = NULL;
Trie() {
}
};
class Solution {
private:
// Root node of dictionary tree
Trie* root = new Trie();
// The highest bit number is 30
int C = 30;
public:
void add(int num) {
Trie* cur = root;
for (int k = C; k >= 0; k--) {
int bit = (num >> k) & 1;
if (bit == 0) {
if (cur->left==NULL) {
cur->left = new Trie();
}
cur = cur->left;
}
else {
if (cur->right==NULL) {
cur->right = new Trie();
}
cur = cur->right;
}
}
}
int check(int num) {
Trie* cur = root;
int x = 0;
for (int k = C; k >= 0; --k) {
int bit = (num >> k) & 1;
if (bit == 0) {
// a_i Of the k Binary bits are 0, Should be used to express 1 Child nodes of right go
if (cur->right) {
cur = cur->right;
x = x * 2 + 1;
}
else {
cur = cur->left;
x = x * 2;
}
}
else {
// a_i Of the k Binary bits are 1, Should be used to express 0 Child nodes of left go
if (cur->left) {
cur = cur->left;
x = x * 2 + 1;
}
else {
cur = cur->right;
x = x * 2;
}
}
}
return x;
}
int findMaximumXOR(vector<int>& nums) {
int n = nums.size();
int x = 0;
for (int i = 1; i < n; ++i) {
// take nums[i-1] Put in dictionary tree , here nums[0 .. i-1] All in the dictionary tree
add(nums[i - 1]);
// take nums[i] regard as ai, Find the biggest x Update answers
x = max(x, check(nums[i]));
}
return x;
}
};
1. Hashtable
class Solution {
public:
int findMaximumXOR(vector<int>& nums) {
int x=0;
// this 31 The binary bits are numbered from low to high 0, 1, ..., 30
//0,1,⋯,30 We went from the top to the top 30 Start with a binary bit , Determine... In turn
//x Every one of you is 0 still 1
for (int k = 30; k >= 0; k--) {
unordered_set<int> seen;
// Will all pre^k(a_j) Put it in the hash table
for (int num: nums) {
// If you just want to keep it from the top to the bottom k The part that ends with two binary bits
// Just move it to the right k position
seen.insert(num >> k);
}
// at present x From the top to the bottom k+1 The part that ends with two binary bits
// We will x Of the k Two binary positions are 1, That is to say x = x*2+1
int x_next = x * 2 + 1;
bool found = false;
// enumeration i
for (int num: nums) {
// or
if (seen.count(x_next ^ (num >> k))) {
found = true;
break;
}
}
if (found) {
x = x_next;
}
else {
// If you don't find one that satisfies the equation a_i and a_j, that x Of the k A binary bit can only be 0
// That is to say x = x*2
x = x_next - 1;
}
}
return x;
}
};
边栏推荐
- Latex various arrows / arrows with text labels / variable length arrows
- 1269. number of options left in place
- Prototype and prototype chain
- [MATLAB image encryption and decryption] chaotic sequence image encryption and decryption (including correlation test) [including GUI source code 1862]
- 服务器调参实录
- Leetcode-141. Linked List Cycle
- Shuttle inside and outside margins
- Shutter restraint container assembly
- Notes on learning Es5 and ES6
- Starting from scratch (IV) enemy aircraft flying out of the border disappear automatically
猜你喜欢
![[deploy private warehouse based on harbor] 3 deploy harbor](/img/cd/be68a430e86b4b23ad93b42a338f00.jpg)
[deploy private warehouse based on harbor] 3 deploy harbor

【概率论与数理统计】猴博士 笔记 p41-44 统计量相关小题、三大分布的判定、性质、总体服从正态分布的统计量小题

【LeetCode】-- 17.电话号码的字母组合

Shuttle container component

VTK-vtkPlane和vtkCutter使用

教育专家王中泽老师:家庭教育重在自己成长

Leetcode hot topic 100 topic 6-10 solution
![[probability theory and mathematical statistics] Dr. monkey's notes p41-44 statistics related topics, determination of three distributions, properties, statistics subject to normal distribution in gen](/img/93/d1014b07c924195e062dc83d69b14a.png)
[probability theory and mathematical statistics] Dr. monkey's notes p41-44 statistics related topics, determination of three distributions, properties, statistics subject to normal distribution in gen

saltstack部署lnmp

byte和bit的区别
随机推荐
Summary of string processing skills II
Cross-Modal Pattern-Propagation for RGB-T Tracking
Drawing with qpainter
RGB-D Salient Object Detection withCross-Modality Modulation and Selection
554. brick wall
[MATLAB image fusion] particle swarm optimization adaptive multispectral image fusion [including source code phase 004]
【Matlab WSN通信】A_Star改进LEACH多跳传输协议【含源码 487期】
Graph Attention Tracking
Required reading 1: the larger the pattern, the better they know how to speak
saltstack部署lnmp
Shell脚本之启动Nacos服务端
WPF data binding (IV)
服务器调参实录
Web API、DOM
Deep Attentive Tracking via Reciprocative Learning
First day of database
Records how cookies are carried in cross domain requests
Leetcode hot topic 100 topic 6-10 solution
byte和bit的区别
【Matlab图像加密解密】混沌序列图像加密解密(含相关性检验)【含GUI源码 1862期】