【特别好】【位运算】maximum-xor-of-two-numbers-in-an-array
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的更多相关文章
- 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 ...
- [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 ...
- [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 ...
- [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 ...
- 【LeetCode】421. Maximum XOR of Two Numbers in an Array 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 依次遍历每一位 前缀树 日期 题目地址:https://lee ...
- 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 ...
- 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 ...
- 421. Maximum XOR of Two Numbers in an Array
这题要求On时间复杂度完成, 第一次做事没什么思路的, 答案网上有不贴了, 总结下这类题的思路. 不局限于这个题, 凡是对于这种给一个 数组, 求出 xxx 最大值的办法, 可能上来默认就是dp, ...
- 421 Maximum XOR of Two Numbers in an Array 数组中两个数的最大异或值
给定一个非空数组,数组中元素为 a0, a1, a2, … , an-1,其中 0 ≤ ai < 231 .找到 ai 和aj 最大的异或 (XOR) 运算结果,其中0 ≤ i, j < ...
- [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 ...
随机推荐
- Next Permutation&&Permutation Sequence
Next Permutation Implement next permutation, which rearranges numbers into the lexicographically nex ...
- Centos7Yum安装配置指定版本nginx
1.安装 rpm -ivh http://nginx.org/packages/centos/7/x86_64/RPMS/nginx-1.14.2-1.el7_4.ngx.x86_64.rpm 2.启 ...
- windows10 自带的OpenSSH Client(Beta)
我不知道其他版本有没有 ,我是windows10 专业版,版本1709,OS内部版本16288.1 安装过程: 1.我的电脑上面的卸载或更改程序 2.管理可选功能 3.添加功能 4.重启电脑,搞定 O ...
- sublime text按esc经常进入command mode(不能输入任何东西)
在使用sublime text进行 选中 操作中,如果使用了esc退出选中状态,会进入command mode,现象是不能输入任何东西,关闭当前编辑文件重新打开可以解决.但是很影响连贯性.可以通过一些 ...
- 关于Vue-cli的跨域解决
由于Vue-cli服务器是跑在node环境下的8080端口,我们的php代码可能在Apache环境下的7070端口,这个时候就会出现跨域 此刻这段php代码在7070端口上 如果直接去访问 页面报错 ...
- 洛谷P1565牛宫
传送门:题目点这里; 首先理解题目,就是要求给定矩阵中权值和不小于零的最大子矩阵,数据范围200也还不算棘手,暴力n^4的算法也可以水到50分.正解要用到单调栈配合二分和前缀和,复杂度n^3logn, ...
- AndroidManifest.xml文件详解(uses-feature)
http://blog.csdn.net/think_soft/article/details/7596796 语法(SYNTAX): <uses-featureandroid:name=&qu ...
- python——聊聊iterable,sequence和iterators
---------------------------------------------------------------前言----------------------------------- ...
- Codeforces Round #300 Quasi Binary(DP)
Quasi Binary time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- Linux修改用户基本信息(不含密码)
如果想修改密码请查看Linux命令之passwd.chpasswd (1).使用usermod修改用户基本信息 Linux命令之usermod (2).进入配置文件修改用户信息 使用vim /etc/ ...