[LeetCode] 421. Maximum XOR of Two Numbers in an Array(位操作)
Description
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.
思路
题意:给定一个数组,在时间复杂度为O(n)条件下求出两数异或的最大值
题解:此方法挺巧妙的,根据异或的性质,如果 a ^ b = c,那么a = b ^ c,因此我们利用这个性质,从高位开始枚举每个数的二进制位的前缀保存下来,然后在每一轮枚举前缀的时候也正好枚举答案,然后再在这些保存下来的前缀中查找是否有两数x, y 异或得到我们枚举的答案。
class Solution {
public:
//162ms
int findMaximumXOR(vector<int>& nums) {
int res = 0,mask = 0;
for (int i = 31;i >= 0;i--){
mask |= (1 << i);
set<int>prefix;
for (unsigned int i = 0;i < nums.size();i++){
prefix.insert(nums[i] & mask);
}
int tmp = res | (1 << i); //从高位枚举答案
//set中的数相当于a、b,tmp相当于c,我们根据tmp和set中已有的一个数异或,
//如果得到的数也在set中,说明这两个数异或能得到tmp值,然后用tmp更新res
for (set<int>::iterator it = prefix.begin();it != prefix.end();it++){
if (prefix.count(tmp^*it)){
res = tmp;
break;
}
}
}
return res;
}
};
[LeetCode] 421. Maximum XOR of Two Numbers in an Array(位操作)的更多相关文章
- [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 ...
- leetcode 421.Maximum XOR of Two Numbers in an Array
题目中给定若干个数,然后任意选定两个数使得其异或值最大. 先利用样例中的: 3 10 5 25 2 8 这些数转换为二进制来看的话那么是先找到最高位的1然后与数组中其他的数相与后的数值保存到set中去 ...
- 【LeetCode】421. Maximum XOR of Two Numbers in an Array 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 依次遍历每一位 前缀树 日期 题目地址:https://lee ...
- 【leetcode】421. Maximum XOR of Two Numbers in an Array
题目如下: 解题思路:本题的难点在于O(n)的复杂度.为了减少比较的次数,我们可以采用字典树保存输入数组中所有元素的二进制的字符串.接下来就是找出每个元素的异或的最大值,把需要找最大值的元素转成二进制 ...
- 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 ...
- 421 Maximum XOR of Two Numbers in an Array 数组中两个数的最大异或值
给定一个非空数组,数组中元素为 a0, a1, a2, … , an-1,其中 0 ≤ ai < 231 .找到 ai 和aj 最大的异或 (XOR) 运算结果,其中0 ≤ i, j < ...
- 421. Maximum XOR of Two Numbers in an Array
这题要求On时间复杂度完成, 第一次做事没什么思路的, 答案网上有不贴了, 总结下这类题的思路. 不局限于这个题, 凡是对于这种给一个 数组, 求出 xxx 最大值的办法, 可能上来默认就是dp, ...
- LeetCode 421. 数组中两个数的最大异或值(Maximum XOR of Two Numbers in an Array) 71
421. 数组中两个数的最大异或值 421. Maximum XOR of Two Numbers in an Array 题目描述 给定一个非空数组,数组中元素为 a0, a1, a2, - , a ...
- [LeetCode] 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 ...
随机推荐
- 使用OFBIZ的理由和不使用OFBIZ的理由
1 使用OFBIZ的理由 1.1 什么是OFBIZ OFBIZ是由Sourceforge维护的一个最著名的开源项目之一,提供创建基于最新J2EE/XML规范和技术标准,构建大型企业级.跨平台.跨数据库 ...
- JSP学习(5)
JSP学习(5) 保存用户状态的两大机制 session对象 Cookie Cookie简介 是Web服务器保存在客户端的一系列文本信息 典型应用 判断注册用户是否已经登录 购物车处理 作用 对特定对 ...
- 【转】 linux硬链接与软链接
转自:http://www.cnblogs.com/yfanqiu/archive/2012/06/11/2545556.html Linux 系统中有软链接和硬链接两种特殊的“文件”. 软链接可以看 ...
- [POI2007]POW-The Flood(并查集)
[POI2007]POW-The Flood Description AKD 市处在一个四面环山的谷地里.最近一场大暴雨引发了洪水,AKD 市全被水淹没了.Blue Mary,AKD 市的市长,召集了 ...
- JuniorCTF - Web - blind
题目链接 https://ctftime.org/task/7450 参考链接 https://github.com/Dvd848/CTFs/blob/master/2018_35C3_Junior/ ...
- window 批处理脚本获取上级目录
1 SET CurrDir=%CD% CD.. SET InstPath=%CD% CD %CurrDir% 2 pushd.. set parent=%cd% popd 参考: https://ms ...
- git如何上传大文件,突破大小限制
Github中单个文件的大小限制是100MB,为了能突破这个限制,我们需要使用Git Large File Storage这个工具, git lfs install git lfs track &qu ...
- 37行代码构建无状态组件通信工具-让恼人的Vuex和Redux滚蛋吧!
状态管理的现状 很多前端开发者认为,Vuex和Redux是用来解决组件间状态通信问题的,所以大部分人仅仅是用于达到状态共享的目的.但是通常Redux是用于解决工程性问题的,用于分离业务与视图,让结构更 ...
- 苹果IOS 12将使您的iPhone更安全,并有更强大的黑客保护
一年一度的IOS刷新正在进行中,苹果已经预览了它,beta测试者已经安装了它,当iPhone在9月份到货时我们其他人应该获得iOS12.虽然软件3-D表情符号和屏幕时间限制等功能在软件到货时可能会受到 ...
- 16.go语言基础学习(上)——2019年12月16日
2019年12月13日10:35:20 1.介绍 2019年10月31日15:09:03 2.基本语法 2.1 定义变量 2019年10月31日16:12:34 1.函数外必须使用var定义变量 va ...