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 result of ai XOR aj, where 0 ≤ i, j < n.

Could you do this in O(n) runtime?

Example:

Input: [3, 10, 5, 25, 2, 8]

Output: 28

Explanation: The maximum result is 5 ^ 25 = 28.

这道题是一道典型的位操作Bit Manipulation的题目,我开始以为异或值最大的两个数一定包括数组的最大值,但是OJ给了另一个例子{10,23,20,18,28},这个数组的异或最大值是10和20异或,得到30。那么只能另辟蹊径,正确的做法是按位遍历,题目中给定了数字的返回不会超过231,那么最多只能有32位,我们用一个从左往右的mask,用来提取数字的前缀,然后将其都存入set中,我们用一个变量t,用来验证当前位为1再或上之前结果res,看结果和set中的前缀异或之后在不在set中,这里用到了一个性质,若a^b=c,那么a=b^c,因为t是我们要验证的当前最大值,所以我们遍历set中的数时,和t异或后的结果仍在set中,说明两个前缀可以异或出t的值,所以我们更新res为t,继续遍历,如果上述讲解不容易理解,那么建议自己带个例子一步一步试试,并把每次循环中set中所有的数字都打印出来,基本应该就能理解了,参见代码如下:

public class Solution {
    public int findMaximumXOR(int[] nums) {
        int mask = 0, max = 0;
        for (int i=31; i>=0; i--) {
            mask |= 1<<i;
            HashSet<Integer> prefixes = new HashSet<Integer>();
            for (int each : nums) {
                prefixes.add(each & mask); // reserve Left bits and ignore Right bits
            }
            int tmpMax = max | (1<<i);
            //possible new max, for example: max right now is 11000, then tmpMax=11100, max is 11100, tmpMax is 11110
            for (int prefix : prefixes) {
                if (prefixes.contains(tmpMax^prefix))
                    max = tmpMax;
            }
        }
        return max;
    }
}

  

Solution 2: Trie, (未研究)

https://discuss.leetcode.com/topic/63207/java-o-n-solution-using-trie

https://discuss.leetcode.com/topic/64753/31ms-o-n-java-solution-using-trie

时间: 2024-12-27 19:47:01

421. Maximum XOR of Two Numbers in an Array的相关文章

[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. Find the maximum result of ai XOR aj, where 0 ≤ i, j < n. Could you do this in O(n) runtime? Example: Input: [3, 10, 5, 25, 2, 8] Output: 28 Explanation: T

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 result of ai XOR aj, where 0 ≤ i, j < n. Could you do this in O(n) runtime? Example: Input: [3, 10, 5, 25, 2, 8] Output: 28 Explanation: The maximum resul

LeetCode Maximum XOR of Two Numbers in an Array

原题链接在这里:https://leetcode.com/problems/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 result of ai XOR aj, where 0 ≤ i, j < n. Could you do this in O(n) runti

[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 result of ai XOR aj, where 0 ≤ i, j < n. Could you do this in O(n) runtime? Example: Input: [3, 10, 5, 25, 2, 8] Output: 28 Explanation: The maximum resul

Maximum Xor Secondary(单调栈好题)

Maximum Xor Secondary CodeForces - 280B Bike loves looking for the second maximum element in the sequence. The second maximum element in the sequence of distinct numbers x1,?x2,?...,?xk (k?>?1) is such maximum element xj, that the following inequalit

Leetcode - 628 Maximum Product of Three Numbers

Leetcode - 628 Maximum Product of Three Numbers 628. Maximum Product of Three Numbers Given an integer array, find three numbers whose product is maximum and output the maximum product. Example 1: Input: [1,2,3] Output: 6 Example 2: Input: [1,2,3,4]

CQUOJ 9906 Little Girl and Maximum XOR

Little Girl and Maximum XOR Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Original ID: 276D64-bit integer IO format: %I64d      Java class name: Any A little girl loves problems on bitwise operations very much.

CF169D2 D – Little Girl and Maximum XOR 贪心

解题思路: 经过打表可得规律答案要么是0 要么是2的N次 要得到最大的XOR值,其值一定是2的N次 即在 l 和 r 的二进制中,从左到右遍历过去,如果碰到 l 为 1 r 为 0 则可说明在『l , r]中存在 1000000000 和 0111111111 可得到最大XOR值为2的N次 PS:不会存在首先出现 l 为 0 r 为 1 的情况,因为 l < r #include<stdio.h> #include<math.h> int main(){ long long

D. Little Girl and Maximum XOR(贪心)

D. Little Girl and Maximum XOR A little girl loves problems on bitwise operations very much. Here's one of them. You are given two integers l and r. Let's consider the values of for all pairs of integers a and b (l?≤?a?≤?b?≤?r). Your task is to find