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.

Solution 1: Bit Manipulation:

这题真心有点难,get the max XOR bit by bit

note that if A^B=C, then A^C=B, B^C=A

 public class Solution {
public int findMaximumXOR(int[] nums) {
int mask = 0, max = 0;
for (int i=31; i>=0; i--) {
mask |= 1<<i;
HashSet<Integer> prefixes = new HashSet<Integer>();
for (int each : nums) {
prefixes.add(each & mask); // reserve Left bits and ignore Right bits
}
int tmpMax = max | (1<<i);
//possible new max, for example: max right now is 11000, then tmpMax=11100, max is 11100, tmpMax is 11110
for (int prefix : prefixes) {
if (prefixes.contains(tmpMax^prefix))
max = tmpMax;
}
}
return max;
}
}

Solution 2: Trie, (未研究)

https://discuss.leetcode.com/topic/63207/java-o-n-solution-using-trie

https://discuss.leetcode.com/topic/64753/31ms-o-n-java-solution-using-trie

Leetcode: Maximum XOR of Two Numbers in an Array的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

  9. [Swift]LeetCode421. 数组中两个数的最大异或值 | 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. HttpClient_使用httpclient必须知道的参数设置及代码写法、存在的风险

    结论: 如果使用httpclient 3.1并发量比较大的项目,最好升级到httpclient4.2.3上,保证并发量大时能抗住.httpclient 4.3.3,目前还有一些bug:还是用4.2.x ...

  2. PHP.ini文件读取不到

    Configuration File (php.ini) Path /usr/local/php/lib Loaded Configuration File (none) Linux 把 dtruss ...

  3. XML 参考:XML基础 XML 简介

    XML 参考:XML基础 -- XML简介和用途 转:http://www.cnblogs.com/Dlonghow/archive/2009/01/22/1379799.html XML 参考:XM ...

  4. HDU1011 树形DP

    Starship Troopers Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  5. html使用心得

    (1)<textarea style= "word-break:break-all" rows="5" cols="20"> 在 ...

  6. 配置DNS实验一例

    1安装bind软件 2查看当前DNS服务 3修改配置文件 4测试

  7. vue 倒计时

    简单粗暴 export default { data () { return { timer: 30, //默认倒数30秒 stop : false, //默认是停止的,但界面加载之后会变成false ...

  8. springmvc源码分析

    Spring MVC源码分析--初始化过程 标签: springmvcconstructioniocclass 2012-09-09 21:32 26578人阅读 评论(3) 收藏 举报 版权声明:本 ...

  9. 关于Response.redirect()方法

    1. sendRedirect 后面要加上return.2. sendRedirect 执行过程是先转向还是先执行后续代码再转向?答: 先执行代码再转向,在一个sendRedirect后面不能再有其他 ...

  10. 【iCore3 双核心板】例程三十:U_DISK_IAP_FPGA实验——更新升级FPGA

    实验指导书及代码包下载: http://pan.baidu.com/s/1jH1TiKY iCore3 购买链接: https://item.taobao.com/item.htm?id=524229 ...