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. java8 引进lamda

    就像泛型能使开发人员对数据类型进行抽象,Lambda的目的是让程序员能够对程序行为进行抽象. 你可以这样想,它能够让程序员把一段程序代码当做数据一样使用.一个方法可以像定义和使用一个变量那样的方式被定 ...

  2. AppStore上传条例

    1. 条款和条件1.1 为App Store开发程序,开发者必须遵守 Program License Agreement (PLA).人机交互指南(HIG)以及开发者和苹果签订的任何协议和合同.以下规 ...

  3. PHP 进行数据庫对比工具

    <?php /** * author jackluo * net.webjoy@gmail.com */ class IMysqlDiff { private $master,$slave; p ...

  4. webservice的Axis2入门教程java版

    本文转自百度文库 Axis2是一套崭新的WebService引擎,该版本是对Axis1.x重新设计的产物.Axis2不仅支持SOAP1.1和SOAP1.2,还集成了非常流行的REST WebServi ...

  5. android-对话框

    一.常用对话框 AlertDialog: 功能最丰富,实际应用最广的对话框(以下三种对话框都是该对话框的子类) ProgressDialog:进度对话框.这个对话框只是对进度条的包装 DatePick ...

  6. mac终端命令大全全全全全全全全全

    OSX 的文件系统 OSX 采用的Unix文件系统,所有文件都挂在跟目录 / 下面,所以不在要有Windows 下的盘符概念. 你在桌面上看到的硬盘都挂在 /Volumes 下. 比如接上个叫做 US ...

  7. 使用 GCC 调试程序

    系统 Ubuntu 调试示例: #include <stdio.h> int func(int n) { ,i; ;i<n;i++) { sum+=i; } return sum; ...

  8. HDU1019

    Least Common Multiple Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Ot ...

  9. 单页Web应用:

    概念: Web应用程序: WEB应用程序一般是B(浏览器)/S(服务器)模式.Web应用程序首先是“应用程序”,和用标准的程序语言,如C.C++等编写出来的程序没有什么本质上的不同.然而Web应用程序 ...

  10. Maven下载安装

    1.maven下载地址 https://maven.apache.org/配置:M2_HOME D:\tool\mvn\apache-maven-3.3.9 PATH %M2_HOME%/bin3.命 ...