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. 3.4 滑动窗口的卷积实现-深度学习第四课《卷积神经网络》-Stanford吴恩达教授
  2. Jupyter notebook与Spyder,以及Jupyter notebook与Spyder集成插件
  3. 字符集和编码规范:ASCII,Unicode和UTF-8, latin1,BIG5,GBK
  4. 解决在Windows10没有修改hosts文件权限
  5. AR9331中Linux内核启动中与IRQ中断相关的文件
  6. Python导函数的一些相关
  7. haproxy + keepalived + mycat 高可用与负载均衡集群配置 centos7
  8. 日志系统新贵 Loki,确实比笨重的 ELK 轻
  9. carry on till tomorrow
  10. wordpress网站被挂马处理
  11. RJ45接头 与 RJ48 接头
  12. Springboot过滤xss
  13. 【乐器常识】声音之美
  14. 阿里云P2P内容分发网络(PCDN)实操手册
  15. 四、Scala从入门到精通一一循环控制
  16. 2022年新消费趋势洞察(护肤篇):时下大热的美妆成分全面复盘
  17. iterm2使用:服务器ssh快捷连接
  18. Django网站实例效果
  19. 数学分析 曲面积分与场论初步(第22章)
  20. 图像、视觉处理的相关代码

热门文章

  1. 【FXCG】亚历山大·埃尔德人生的传奇色彩。
  2. Git(仓库,分支,生成SSH公钥,IDEA集成Git)
  3. rpm安装软件时提示warning::Header V3 RSA/SHA256 Signature, key ID ec551f03: NOKEY
  4. 关闭新版火狐浏览器无障碍模式
  5. html5实现3d正方形表情旋转,HTML5 带面部表情的拟人化立方体
  6. 虚拟机VM安装linux以及网卡配置
  7. 网易云音乐喊话酷狗,内涵后者疑似抄袭部分功能
  8. 初次学习绘画怎么绘画好场景人物?场景画人物的作用怎样画?
  9. 小白,你要的Java抽象类,操碎了心!
  10. 史上最强数据结构----算法的时间复杂度和空间复杂度