传送门

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. 进程通信(multiprocessing.Queue)

    from multiprocessing import Queue Queue([maxsize]) 创建共享的进程队列.maxsize是队列中允许的最大项数.如果省略此参数,则无大小限制.底层队列使 ...

  2. Kali系统 metasploit 使用教程

    基础配置 由于kali 2.0 已经没有metasploit 这个服务了,所以service metasploit start 的方式不起作用. 在kali 2.0中启动带数据库支持的MSF方式如下: ...

  3. MySQL 5.7安装(linux)

    https://blog.csdn.net/li_Dijkstra/article/details/79354385 https://blog.csdn.net/li_Dijkstra/article ...

  4. CSS的重用

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

  5. the sum of two fixed value

    the sum of two fixed value description Input an array and an integer, fina a pair of number in the a ...

  6. ASE Backend Alpha Sprint Review

    [Backend] Alpha Review展示博客 团队成员介绍:仅限于Alpha阶段有贡献的成员. 典型场景描述:描述并说明你们认为的产品面向的典型场景. 团队管理与协作:包括但不限于团队内部如何 ...

  7. EwoMail 邮件服务器安装

    ewomail 安装及使用 主页:http://www.ewomail.com/ 开源版主页:http://www.ewomail.com/list-9.html 开源版文档:http://doc.e ...

  8. 01scikit-learn数据集下载

    In [2]: from sklearn.datasets import load_iris iris = load_iris() iris.keys() Out[2]: dict_keys(['da ...

  9. 3.xml的解析

    1.xml的解析原理简介(xml是标记型文档) (1)js使用dom解析标记型文档(html)? - 根据html的层级结构,在内存中分配一个树形结构,把html的标签,属性和文本都封装成对象 - d ...

  10. 用于理解C++右值引用的例子

    #include <iostream> using namespace std; void printReference (int& value) { cout << ...