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. u-boot: Not enough room for program headers, try linking with -N

    在编译u-boot的时候出现了以下错误: arm-linux-gnueabi-ld.bfd: u-boot: Not enough room for program headers, try link ...

  2. OpenCV Error: Unspecified Error(The Function is not implemented)

    Ubuntu 或者 Debian 系统显示窗口的时候遇到了这个问题 error: (-2:Unspecified error) The function is not implemented. Reb ...

  3. [coj 1353 Guessing the Number]kmp,字符串最小表示法

    题意:给一个字符串,求它的最小子串,使得原串是通过它重复得到的字符串的一个子串. 思路:先求最小长度,最小循环长度可以利用kmp的next数组快速得到,求出长度后然后利用字符串最小表示法求循环节的最小 ...

  4. [zoj3593]扩展欧几里得+三分

    题意:给一个数A,有6种操作,+a,-a,+b,-b,+(a+b),-(a+b),每次选择一种,用最少的次数变成B. 思路:由于不同的操作先后顺序对最后的结果没有影响,并且加一个数与减一个相同的数不能 ...

  5. Jmeter-函数助手之${__RandomString(,,)}使用

    ${__RandomString(,,)}使用方法 1.在日常写脚本中,可以随机生成指定的几个字符串作为入参的value,那么jmeter 的这个工具就特别好用.  应用: 2.填写接口入参, 3.运 ...

  6. Python实现将网站域名解析为ip地址

    起因 因为一些事情,需要将域名解析为ip地址,想到Python作为万能语言,就用Python来实现这个功能 代码 import socket url = 'shiyixirui.cn' res = s ...

  7. STL库中神奇函数nth_element

    用法:nth_element(数组名,数组名+第k小元素,数组名+元素个数) 这个函数主要用来将数组元素中第k小的整数排出来并在数组中就位,随时调用. 例如: ]={,,,,},k ; cin> ...

  8. 200万年薪请不到!清华姚班到底有多牛X?

    前几天,清华大学自动化系2020年大一新生的C++作业因为太难而上了热搜,该话题在知乎上的热度一度高达 1300+ 万.  在该帖子下方,有很多关于这件事的讨论,其中很多不禁赞叹"清华太牛 ...

  9. vim命令备份

    vim命令 vim键盘位置说明 在命令状态下对当前行用 == (连按=两次), 或对多行用 n==(n是自然数)表示自动缩进从当前行起的下面n行. 可以试试把代码缩进任意打乱再用 n== 排版,相当于 ...

  10. H3C S5500V2交换机误格式化恢复

    一.格式化后,bin文件及视图全部被删除需要联系H3C客服报交换机后面的序列号,然后根据工单中给你的账号密码去H3C官网下载对应的软件包. 二.下载3CDaemon使用TFTP方式将解压出来的.ipe ...