传送门

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 ≤ ij < 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(位操作)的更多相关文章

  1. [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 ...

  2. leetcode 421.Maximum XOR of Two Numbers in an Array

    题目中给定若干个数,然后任意选定两个数使得其异或值最大. 先利用样例中的: 3 10 5 25 2 8 这些数转换为二进制来看的话那么是先找到最高位的1然后与数组中其他的数相与后的数值保存到set中去 ...

  3. 【LeetCode】421. Maximum XOR of Two Numbers in an Array 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 依次遍历每一位 前缀树 日期 题目地址:https://lee ...

  4. 【leetcode】421. Maximum XOR of Two Numbers in an Array

    题目如下: 解题思路:本题的难点在于O(n)的复杂度.为了减少比较的次数,我们可以采用字典树保存输入数组中所有元素的二进制的字符串.接下来就是找出每个元素的异或的最大值,把需要找最大值的元素转成二进制 ...

  5. 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 ...

  6. 421 Maximum XOR of Two Numbers in an Array 数组中两个数的最大异或值

    给定一个非空数组,数组中元素为 a0, a1, a2, … , an-1,其中 0 ≤ ai < 231 .找到 ai 和aj 最大的异或 (XOR) 运算结果,其中0 ≤ i,  j < ...

  7. 421. Maximum XOR of Two Numbers in an Array

    这题要求On时间复杂度完成, 第一次做事没什么思路的, 答案网上有不贴了, 总结下这类题的思路. 不局限于这个题, 凡是对于这种给一个  数组,  求出 xxx 最大值的办法, 可能上来默认就是dp, ...

  8. 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 ...

  9. [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 ...

随机推荐

  1. timeout使用实例

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. 自制悬浮框,愉快地查看栈顶 Activity

    接手陌生模块时,如何快速了解每个页面对应的类,以及它们之间的跳转逻辑.总不能在代码里一个一个地找startActivity()吧? 有时候,又想查看别人的 app 的页面组织(像淘宝.微信啊),总不能 ...

  3. Solr的学习使用之(十)数据库(Oracle、SqlServer)原有的全文索引功能和Solr对比?

    本人有个问题一直不解,既然solr的全文索引功能这么强大,而且效果也不错,那为什么那些数据库厂商比如Oracle.SqlServer,不把solr的功能集成进去呢,或者说把全文索引的功能做好点,做到和 ...

  4. 2018-9-21-dot-net-core-使用-usb

    title author date CreateTime categories dot net core 使用 usb lindexi 2018-09-21 19:53:34 +0800 2018-0 ...

  5. VS #include 【bits/bstdc++.h】出错

    目录 1. 本文地址 2. 按 3. 操作步骤 1. 本文地址 博客园:https://www.cnblogs.com/coco56/p/11163142.html 简书:https://www.ji ...

  6. Python not and or

    刷题时候,有道题目的答案是 return(num and (num % 9 or 9)) 看的有点懵逼,看来解释如下: 1.首先,’and’.’or’.’not’的优先级是not>and> ...

  7. Jenkins构建触发器(定时构建项目)

    如上图所示,Jenkins通常通过点击“立即构建”来进行手动构建项目,其实也可以使用配置中的 Poll SCM和Build periodically来进行定时自动构建项目: 在“配置”——>“构 ...

  8. LOJ6300 BZOJ5283 [CodePlus 2018 3 月赛]博弈论与概率统计

    一道好题!很久以前就想做了,咕到了现在,讲第二遍了才做. 首先我们观察到$p$是没有用的 因为赢的次数一定 那么每一种合法序列出现的概率均为$p^n*(1-p)^m$ 是均等的 我们可以不看它了 然后 ...

  9. [HYSBZ - 3252] 攻略

    问题描述 题目简述:树版[k取方格数] 众所周知,桂木桂马是攻略之神,开启攻略之神模式后,他可以同时攻略k部游戏.今天他得到了一款新游戏<XX 半岛>,这款游戏有n个场景(scene),某 ...

  10. 【leetcode】447. Number of Boomerangs

    题目如下: 解题思路:我首先用来时间复杂度是O(n^3)的解法,会判定为超时:后来尝试O(n^2)的解法,可以被AC.对于任意一个点,我们都可以计算出它与其余点的距离,使用一个字典保存每个距离的点的数 ...