421. Maximum XOR of Two Numbers in an Array详解
题目:
Given a non-empty array of numbers, a0, a1, a2, … , an-1, where 0 ≤ ai < 231.
Find the maximum result of ai XOR aj, where 0 ≤ i, j < n.
Could you do this in O(n) runtime?
Example:
Input: [3, 10, 5, 25, 2, 8]
Output: 28
Explanation: The maximum result is 5 ^ 25 = 28
代码:
#include<iostream>
#include<set>
using namespace std;
int findMaximumXOR(int *nums,int a) {
set<int>::iterator it1;
for(int i=0; i<6; i++){
cout<<nums[i]<<endl;
}
int max = 0, mask = 0;
for(int i = 31; i >= 0; i--){
mask = mask | (1 << i);
set<int> set1;
for(int j=0; j<6; j++){
// cout<<"nums[i]="<<nums[i]<<"; mask="<<mask<<"; nums[i]&mask="<<(nums[i]&mask)<<endl;
set1.insert(nums[j] & mask);
}
cout<<"bianli start;"<<endl;
for (it1 = set1.begin(); it1 != set1.end(); ++it1)
{
cout << *it1 << endl;
}
cout<<"bianli end;"<<endl;
int tmp = max | (1 << i);
for(set<int>::iterator it = set1.begin(); it != set1.end() ;it++){
if(set1.end()!=set1.find(tmp ^ (*it))) {
cout<<"tmp="<<tmp<<"; tmp^(*it)="<<(tmp^(*it))<<"; *it="<<(*it)<<endl;
max = tmp;
break;
}
}
cout<<endl;
}
return max;
};
int main(){
int nums[] = {
2,3,5,10,8,25
};
int max = findMaximumXOR(nums,6);
cout<<max;
}
解释:通过mask从左往右和nums中的数&,mask从左往右依次增加1位。
set1中的数据为nums中的数分别与mask &之后的值,即nums中的数被从左往右依次处理。
tmp为当前max和新的一位或运算,然后通过下面的循环判断tmp是否为合适的max
如果tmp能与set1中的某一个数异或后还存在于set1中的话,那么set1中的这两个相关数异或后就可以成为tmp,即当前最大值。然后就可以把它赋值给max。
这里用了异或的自反性,即 a ^ b = c,而a ^ b ^ b = a, 则 c ^ b = a。理解这一点很重要。
说明:我的代码只是用于例子,不具有通用性,需要改一改。
421. Maximum XOR of Two Numbers in an Array详解相关推荐
- [leetcode]421. Maximum XOR of Two Numbers in an Array
421. Maximum XOR of Two Numbers in an Array Given a non-empty array of numbers, a0, a1, a2, - , a
- 421. Maximum XOR of Two Numbers in an Array——本质:利用trie数据结构查找
Given a non-empty array of numbers, a0, a1, a2, - , an-1, where 0 ≤ ai < 231. Find the maximum re ...
- leetcode 421. Maximum XOR of Two Numbers in an Array
Given a non-empty array of numbers, a0, a1, a2, - , an-1, where 0 ≤ ai < 231. Find the maximum re ...
- 421. Maximum XOR of Two Numbers in an Array
Given a non-empty array of numbers, a0, a1, a2, - , an-1, where 0 ≤ ai < 231. Find the maximum re ...
- leetcode 421. Maximum XOR of Two Numbers in an Array | 421. 数组中两个数的最大异或值(位运算,Trie前缀树)
题目 https://leetcode.com/problems/maximum-xor-of-two-numbers-in-an-array/ 题解 自己没有思路,看了答案之后自己写的. 参考:Py ...
- 421. Maximum XOR of Two Numbers in an Array [Medium]
自己不会做,连解答都看的比较吃力 对位运算还是不太熟悉,常见技巧在位运算场景中容易用不出来 一姐讲解视频: https://www.bilibili.com/video/BV1pr4y1c7B8?fr ...
- LeetCode 421. Maximum XOR of Two Numbers in an Array--Python解法
LeetCode 421. Maximum XOR of Two Numbers in an Array–C++,Python解法 LeetCode题解专栏:LeetCode题解 我做的所有的Leet ...
- 【LeetCode-421】Maximum XOR of Two Numbers in an Array
Given a list of numbers, a[0], a[1], a[2], - , a[N-1], where 0 <= a[i] < 2^32. Find the maximu ...
- LeetCode Maximum XOR of Two Numbers in an Array(贪心、字典树)
问题:给出一个非空的整数数组,找到最大的两个数的异或值.要求时间复杂度为O(n) 思路:根据二进制表示的前缀. 先求出最大数的位数,然后根据位数作位操作异或判断最大值.由于要求时间复杂度为O(n),在 ...
最新文章
- retinaface tensorRT
- 数据库SQL的分组函数
- mysql 备库 hang住_mysql主键的缺少导致备库hang住
- 小型数控雕刻机制作Arduino_开一家全屋定制装修公司,怎么选择开料机与雕刻机?...
- Linux nano编辑txt文件,Linux 文本编辑器 nano 的简单使用
- 【WP8.1开发】RenderTargetBitmap类的特殊用途
- Java基础夺命连环16问
- github上传_上传本地代码到github
- 你最缺钱的时候是怎么度过的?
- MySQL 时间戳转换成秒
- 服务器做raid bios界面做raid配置
- Windows8 10设置程序为 系统默认浏览器
- java编辑双层pdf文件,双层PDF制作系统
- 从我的历程谈谈该如何学习
- scipy--统计检验
- FFmpeg一些感想
- 协议分析(微信网页版 wx2.qq.com)
- 中国人工智能领域企业分类(附未来企业排行)
- 现实赢了袖手旁观他在冷眼看我们
- 删除腾讯电脑管家的卸载残余qmbsrv