421. 数组中两个数的最大异或值

给定一个非空数组,数组中元素为 a0, a1, a2, … , an-1,其中 0 ≤ ai < 231 。

找到 ai 和aj 最大的异或 (XOR) 运算结果,其中0 ≤ i, j < n 。

你能在O(n)的时间解决这个问题吗?

示例:

输入: [3, 10, 5, 25, 2, 8]

输出: 28

解释: 最大的结果是 5 ^ 25 = 28.

PS:

前缀树

class Solution {

    class TrieNode {

        TrieNode zero;

        TrieNode one;

        int val;

        TrieNode() {

        }
} class Trie { TrieNode root; Trie() {
root = new TrieNode();
} void insert(int num) {
TrieNode node = root;
for (int i = 31; i >= 0; i--) {
int bit = num & (1 << i);
if (bit == 0) {
if (node.zero == null) {
node.zero = new TrieNode();
}
node = node.zero;
} else {
if (node.one == null) {
node.one = new TrieNode();
}
node = node.one;
}
}
node.val = num;
} int find(int num) {
TrieNode node = root;
for (int i = 31; i >= 0; i--) {
int bit = num & (1 << i);
if (bit == 0) {
node = node.one == null ? node.zero : node.one;
} else {
node = node.zero == null ? node.one : node.zero;
}
}
return node.val;
}
} // 数组中两个数的最大异或值
public int findMaximumXOR(int[] nums) {
Trie trie = new Trie();
for (int num : nums) {
trie.insert(num);
}
int res = 0;
for (int num : nums) {
int other = trie.find(num);
res = Math.max(res, num ^ other);
}
return res;
}
}

Java实现 LeetCode 421 数组中两个数的最大异或值的更多相关文章

  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 421.数组中两数的最大异或值

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

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

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

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

  5. LeetCode 数组中两个数的最大异或值

    题目链接:https://leetcode-cn.com/problems/maximum-xor-of-two-numbers-in-an-array/ 题目大意: 略. 分析: 字典树 + 贪心. ...

  6. leetcode-421-数组中两个数的最大异或值*(前缀树)

    题目描述: 方法一: class Solution: def findMaximumXOR(self, nums: List[int]) -> int: root = TreeNode(-1) ...

  7. 2016网易实习生编程题:数组中两个数的和等于sum

    题目 找出数组中两个数的和等于sum的这两个数 解题 这个题目做过很多次了,利用HashMap,key为 sum-A[i] value为 i 当 加入HashMap时候A[i] 已经存在map中,ge ...

  8. Java泛型01--任意数组中两元素交换

    package com.zl.generic; /** * 交换“任意”数组 中两个元素 */ public class GenericSwapArray { public static void m ...

  9. 在O(N)时间内求解 正数数组中 两个数相加的 最大值

    一,问题描述 给定一个正数数组arr(即数组元素全是正数),找出该数组中,两个元素相加的最大值,其中被加数的下标大于加数的下标.由加法运算的可逆性,j >i 这个条件可以去掉. 即求出: max ...

随机推荐

  1. 关于 k210 的 micropython 添加 ussl 模块,实现 https 访问支持的那些事。

    起因 事情已经过去快一周了吧,继上次修复 maixpy k210 的 esp8285 at 通信后,突然遇到泽畔大大问,要不要做 ussl 的支持? 评估了一下各方的实现,想了一下自己也刚好在做网络层 ...

  2. Openwrt:添加"自定义软件包.ipk"

    我们已经尝试做的一件事情,是让移植软件到OpenWrt的操作变得非常容易.如果打开OpenWrt里的一个软件包的目录(OpenWrt/Package/* 或 OpenWrt/feeds/package ...

  3. android 动态设置TextView值,例:金额增加

    一说到动态递增设置TextView值,很多人应该马上就想到起个线程,让后在线程中睡眠指定时间,使用handler发送消息更新TextView值! 这样是实现了动态递增设置TextView值但是效率不咋 ...

  4. Spring源码解析02:Spring IOC容器之XmlBeanFactory启动流程分析和源码解析

    一. 前言 Spring容器主要分为两类BeanFactory和ApplicationContext,后者是基于前者的功能扩展,也就是一个基础容器和一个高级容器的区别.本篇就以BeanFactory基 ...

  5. sublime text 3 关联鼠标右击

    比如有网上有如下方法 https://my.oschina.net/adairs/blog/466777 , 其实一个更简单的方法是运行sublime setup.exe , 直接会有相关提示,下一步 ...

  6. Angular知识点复习

    Angular第三方UI组件库(github搜“awesome angular ")-----lonic 概述:是一个第三方的适用于移动端App的UI组件库,可以与Angular/React ...

  7. Gym101612L Little Difference

    题目链接:https://vjudge.net/problem/Gym-101612L 知识点: 数学 题目大意: 给一个数 \(n(1 \le n \le 10^{18})\),要求将 \(n\) ...

  8. 【MySQL】MySQL5.7等以上版本在Windows上的配置

    由于本人是win10系统,所以说下win10系统以管理员身份打开cmd 1. 配置环境变量 我这边是安装在了C:\Program Files\MySQL\MySQL Server 5.7在path中加 ...

  9. freemark+dom4j实现自动化word导出

    导出word我们常用的是通过POI实现导出.POI最擅长的是EXCEL的操作.word操作起来样式控制还是太繁琐了.今天我们介绍下通过FREEMARK来实现word模板导出. 目录 开发准备 模板准备 ...

  10. 如何在Teamcenter中使用PMI?

    1 .什么是PMI 在设计制造领域,PMI指的是产品制造信息(Productand Manufacturing Information),其目的在于在三维环境下,将制造信息从设计部门传递到制造部门.其 ...