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: 28Explanation: The maximum result is 5 ^ 25 = 28.

思路:按照位来算

/** 异或出来的结果就是31!种* 实际上我们可以一个bit一个bit来,这样可以减少一点重复计算* 比如11....是不行的,那么11..1..就不要算了*/
public class Solution {public int findMaximumXOR(int[] nums) {int prefix = 0, max = 0;for(int i=31; i>=0; i--) {Set<Integer> set = new HashSet<Integer>();prefix = prefix | (1<<i);for(int num : nums)set.add(num & prefix);// 计算可能的更大值,基于以下定理:a^b^b=a,// 如果set里面有2个数异或可以得到possibleMax,那么possibleMax和set里的一个数异或得到的就是set里面的另外一个数int possibleMax = max | (1<<i);for(int num : set)if(set.contains(possibleMax ^ num)) {max = possibleMax;break;}}return max;}
}

Trie树版本

/** 需要两两比较,又是按照一位一位来计算的,所以考虑Trie* 总体思路还是按位来算*/
public class trie1 {class Trie {Trie[] children = new Trie[2];}public int findMaximumXOR(int[] nums) {// build up TrieTrie root = new Trie();for(int num : nums) {Trie cur = root;for(int i=31; i>=0; i--) {int bit = (num >> i) & i;if(cur.children[bit] == null)cur.children[bit] = new Trie();cur = cur.children[bit];}}// find bit by bitint max = Integer.MIN_VALUE;for(int num : nums) {Trie cur = root;int sum = 0;for(int i=31; i>=0; i--) {int bit = (num >> i) & i;int xor = bit ^ 1;if(cur.children[xor] != null) {cur = cur.children[xor];sum += (1 << i);} else if(cur.children[bit] != null)cur = cur.children[bit];}max = Math.max(max, sum);}return max;}
}

用Object数组表示的Trie树版本

/** 新建Class Trie会超时*/
public class Solution {public int findMaximumXOR(int[] nums) {// build up TrieObject[] root = {null, null};for(int num : nums) {Object[] cur = root;for(int i=31; i>=0; i--) {int bit = (num >> i) & 1;if(cur[bit] == null)cur[bit] = new Object[]{null, null};cur = (Object[]) cur[bit];}}// find bit by bitint max = Integer.MIN_VALUE;for(int num : nums) {Object[] cur = root;int sum = 0;for(int i=31; i>=0; i--) {int bit = (num >> i) & 1;int xor = bit ^ 1;if(cur[xor] != null) {cur = (Object[]) cur[xor];sum += (1 << i);} else if(cur[bit] != null)cur = (Object[]) cur[bit];}max = Math.max(max, sum);}return max;}
}

2刷:一般这样的bit运算都有个套路就是要按位来求,反正int就32位,

求到第i位时判断有没有2个数的前缀异或得到当前的最大值,想到了前缀,Trie树就自然而然想到了

/** 按位来求* 求到第i位时判断有没有2个数的前缀异或得到当前的最大值*/
public class Solution {public int findMaximumXOR(int[] nums) {int max = 0, prefix = 0;for(int i=31; i>=0; i--) {// 把前缀放到set里面prefix = prefix | (1<<i);Set<Integer> s = new HashSet<Integer>();for(int num : nums)        s.add(num & prefix);int tmp = max | (1<<i);for(int num : s)if(s.contains(tmp ^ num)) {   // 有2个前缀可以异或成当前可能的最大值max = tmp;break;}}return max;}
}

421. Maximum XOR of Two Numbers in an Array相关推荐

  1. [leetcode]421. Maximum XOR of Two Numbers in an Array

    421. Maximum XOR of Two Numbers in an Array Given a non-empty array of numbers, a0, a1, a2, - , a

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

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

  5. leetcode 421. Maximum XOR of Two Numbers in an Array | 421. 数组中两个数的最大异或值(位运算,Trie前缀树)

    题目 https://leetcode.com/problems/maximum-xor-of-two-numbers-in-an-array/ 题解 自己没有思路,看了答案之后自己写的. 参考:Py ...

  6. 421. Maximum XOR of Two Numbers in an Array [Medium]

    自己不会做,连解答都看的比较吃力 对位运算还是不太熟悉,常见技巧在位运算场景中容易用不出来 一姐讲解视频: https://www.bilibili.com/video/BV1pr4y1c7B8?fr ...

  7. LeetCode 421. Maximum XOR of Two Numbers in an Array--Python解法

    LeetCode 421. Maximum XOR of Two Numbers in an Array–C++,Python解法 LeetCode题解专栏:LeetCode题解 我做的所有的Leet ...

  8. 【LeetCode-421】Maximum XOR of Two Numbers in an Array

    Given a list of numbers, a[0], a[1], a[2], - , a[N-1], where 0 <= a[i] < 2^32. Find the maximu ...

  9. LeetCode Maximum XOR of Two Numbers in an Array(贪心、字典树)

    问题:给出一个非空的整数数组,找到最大的两个数的异或值.要求时间复杂度为O(n) 思路:根据二进制表示的前缀. 先求出最大数的位数,然后根据位数作位操作异或判断最大值.由于要求时间复杂度为O(n),在 ...

最新文章

  1. SegmentFault 专访 | AlloyTeam 2015 前端技术大会讲师圆桌
  2. Elias-Fano编码算法——倒排索引压缩用,本质上就是桶排序数据结构思路
  3. mysql 的 VARCHAR VARCHAR2
  4. java 8 并行_Java 8新特性之 并行和并行数组(八恶人-8)
  5. 【渝粤题库】陕西师范大学165209 组织职业生涯管理 作业(专升本)
  6. 如何看屈曲因子_Abaqus 非线性屈曲分析方法
  7. 笔记本交还公司了,伴随了我两年的家伙。
  8. android播放音频的格式,android 音频播放_android ios 音频格式_android 播放网络音频...
  9. DB2-SQLCODE 错误码大全---[IBM官方]
  10. 《软件工程导论第6版》--张海藩 牟永敏 课后答案及其详解 第4章 形式化说明技术
  11. VBA编程之ODBC连接数据库
  12. mysql数据库首次查询缓慢
  13. 信息安全意识教育日历——By 安全牛
  14. 安卓或苹果IOS的APP应用如何取名字?好的名字技巧?
  15. 1945:【09NOIP普及组】多项式输出
  16. 牛市来了,直接买币不就OK了,干嘛非要上矿机?
  17. 使用unity3D实现全景图像查看器
  18. android vitamio集成教程,集成Vitamio实现万能播放器(示例代码)
  19. 软件测试的定义、分类、方法、生命周期
  20. [转载] K3漏油器全紫铜替换原硅胶垫教程。标准姿势

热门文章

  1. React Developer Tools Chrome浏览器扩展一直提示“This page doesn’t appear to be using React.“最新解决办法
  2. 计算机毕设(附源码)JAVA-SSM乐器销售管理系统
  3. cmd进入文件夹/操作+解决路径中的空格问题
  4. java实现打手机电话功能_Android 实现手机拨打电话的功能
  5. 语音识别——一份简短的技术综述
  6. 一种精准monkey测试的方法
  7. 【python】多图片合并PDF
  8. greg名字寓意_主题演讲-Greg Gilley与Adobe
  9. 每日曝光超500次,中国人的脸还能自己做主吗?
  10. 有没有一款没有弹窗、没有广告的浏览器?Speedceo浏览器不就是吗!