leetcode421

public class Solution {
    public int FindMaximumXOR(int[] nums) {
        int max = 0, mask = 0;
            for (int i = 31; i >= 0; i--)
            {
                mask = mask | (1 << i);
                HashSet<int> set = new HashSet<int>();
                foreach (int num in nums)
                {
                    set.Add(num & mask);
                }
                int tmp = max | (1 << i);
                foreach (int prefix in set)
                {
                    if (set.Contains(tmp ^ prefix))
                    {
                        max = tmp;
                        break;
                    }
                }
            }
            return max;
    }
}

https://leetcode.com/problems/maximum-xor-of-two-numbers-in-an-array/#/description

时间: 2024-10-12 16:16:35

leetcode421的相关文章

[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