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. 17:django Email

    我自己在看这一节之前自己先实现了一下,django-admin.py startproject testEmail新建一个新项目,urls.py把urlpatterns的第一行取消注释,改成url(r ...

  2. gridcontrol的列头右键菜单问题

    Dev控件GridControl设置了一个右键菜单 this.gridControl1.ContextMenu = contextMenu2; 而GridControl在运行排序的时候,即 gridv ...

  3. 安装vmware+CentOS 7.4

    安装步骤 选择第一个 按tab键 空格下一行 输入 红框内容  回车 注意事项 道路不通排查过程1.ip地址2.vmware 编辑-虚拟网络编辑器3.windows 服务 vmware相关服务 要开启 ...

  4. angularJS的MVC的用法

    1.前端MVC: M:Model,数据库 V:HTML页面 C:Control控制器 比较很有名的前端MVC框架:ExtJs 2.angularJS的MVC框架搭建 index.html代码如下: & ...

  5. [水煮 ASP.NET Web API2 方法论](1-2)在 WebForm 应用程序中添加 ASP.NET Web API

    问题 怎么样将 Asp.Net Web Api 加入到 Asp.Net Web From 应用程序中 解决方案 在 Visual Studio 2013 中,创建新的 Web From,可以直接在&q ...

  6. JDBC连接执行mysql存储过程报权限错误:User does not have access to metadata required to determine stored procedure parameter types. If rights can not be granted,

    分析:JDBC在调用存储过程时不光用户要有execute的权限,还需要对mysql.proc具有访问权限.否则它无法访问metadata 解决方案:给数据库用户赋权,赋执行mysql.proc表的se ...

  7. centos 磁盘分区格式化与挂载

    1 查看系统里硬盘信息fdisk -l 2 磁盘分区fdisk /dev/sdc输入m显示帮助a:命令指定启动分区:d:命令删除一个存在的分区:l:命令显示分区ID号的列表:m:查看fdisk命令帮助 ...

  8. Qt不同版本编译器,调用VC++生成的动态链接库

    今天用QT编译生成的共享库自己却怎么都不能调用,查了N久后找到这个帖子,发现搞定了,记录一下 http://qiusuoge.com/12720.html Qt如何调用VC++生成的动态链接库?假设当 ...

  9. Linux命令之find(二)

    接上一篇Linux命令之find(一) (1).实例 1.列出当前目录下及子目录下所有的.txt文件 [xf@xuexi ~]$ ls 1.txt 3.txt b.txt 公共 视频 文档 音乐 2. ...

  10. Linux命令之route

    route [-CFvnNee] [-A family] [-4|-6] route [-v] [-A family] [-4|-6] add [-net|-host] target [netmask ...