https://leetcode.com/problems/maximum-xor-of-two-numbers-in-an-array/

利用了异或的”自反性“: a ^ b = c,而a ^ b ^ b = a, 则 c ^ b = a

其他运算定律有:交换律、结合律、分配律。

注意:计算使用的结果,不是只看一位,而是每次把新的一位加到原来的结果后面。这样的好处是不需要记录之前的结果满足条件的有哪些,每次就重新计算和查找就可以了,大大降低了复杂度。

// 非常非常棒
// 参考了 https://discuss.leetcode.com/topic/63213/java-o-n-solution-using-bit-manipulation-and-hashmap
// 特别的,利用了异或的强大运算特性,见22行,来加速运算 public class Solution {
public int findMaximumXOR(int[] nums) {
int max = 0;
int flag = 0; // from left to right
for (int i=31; i>=0; i--) {
Set<Integer> prefixSet = new HashSet();
// flag : 11110000
flag = flag | (1<<i);
for (int num : nums) {
prefixSet.add(num & flag);
} // tmp, max: 10101000000, add more 1
int tmp = max | (1<<i);
for (int prefix : prefixSet) {
// 利用了 ^ 的 a ^ b = c,则 b ^ c = a
if (prefixSet.contains(tmp ^ prefix)) {
max = tmp;
break;
}
}
}
return max;
}
}

【特别好】【位运算】maximum-xor-of-two-numbers-in-an-array的更多相关文章

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

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

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

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

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

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

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

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

  8. 421. Maximum XOR of Two Numbers in an Array

    这题要求On时间复杂度完成, 第一次做事没什么思路的, 答案网上有不贴了, 总结下这类题的思路. 不局限于这个题, 凡是对于这种给一个  数组,  求出 xxx 最大值的办法, 可能上来默认就是dp, ...

  9. 421 Maximum XOR of Two Numbers in an Array 数组中两个数的最大异或值

    给定一个非空数组,数组中元素为 a0, a1, a2, … , an-1,其中 0 ≤ ai < 231 .找到 ai 和aj 最大的异或 (XOR) 运算结果,其中0 ≤ i,  j < ...

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

随机推荐

  1. setTimeout(fn,0)

    我们都知道setTimeout是一个延迟执行的函数 console.log(); setTimeout(function(){console.log();},); console.log(); 会得到 ...

  2. Django Ajax学习二之文件上传

    基于Django实现文件上传 1. url路由配置 路径:C:\Users\supery\Desktop\day82\demo_ajax\demo_ajax\urls.py from django.c ...

  3. python中的偏函数partial

    Python的functools模块提供了很多有用的功能,其中一个就是偏函数(Partial function).要注意,这里的偏函数和数学意义上的偏函数不一样. 在介绍函数参数的时候,我们讲到,通过 ...

  4. hdu 1664(数论+同余搜索+记录路径)

    Different Digits Time Limit: 10000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  5. Binary Tree Preorder Traversal——经典算法的迭代求解(前序,中序,后序都在这里了)

    先序遍历,用递归来做,简单的不能再简单了.代码如下: (以下仅实现了先序遍历,中序遍历类似,后序遍历和这两个思路不一样,具体详见Binary Tree Postorder Traversal) /** ...

  6. cordova热更新插件的使用

    cordova常用命令  一.添加插件 新建Cordova项目 cordova create CordovaHotCode com.ezample.hotcode 添加Android平台 在新建项目的 ...

  7. Android,几款apk工具

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha 可以查看到链接的设备 可以查看到wifi密码,这个也是个开源的 可以查询驾照学时进度 可 ...

  8. Java 线程池的实现

    http://blog.csdn.net/iterzebra/article/details/6758481 http://blog.sina.com.cn/s/blog_4914a33b010118 ...

  9. BZOJ 3544 [ONTAK2010]Creative Accounting(set)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3544 [题目大意] 找一段区间使得Σai mod m的值最大. [题解] 首先计算前缀 ...

  10. 【高斯消元】CDOJ1784 曜酱的线性代数课堂(二)

    高斯消元求矩阵秩板子. #include<cstdio> #include<cmath> #include<algorithm> #include<cstri ...