forked from BigEggStudy/LeetCode-CS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0421-MaximumXOROfTwoNumbersInAnArray.cs
More file actions
37 lines (34 loc) · 1.04 KB
/
0421-MaximumXOROfTwoNumbersInAnArray.cs
File metadata and controls
37 lines (34 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
//-----------------------------------------------------------------------------
// Runtime: 152ms
// Memory Usage: 25.3 MB
// Link: https://leetcode.com/submissions/detail/396474546/
//-----------------------------------------------------------------------------
using System.Collections.Generic;
namespace LeetCode
{
public class _0421_MaximumXOROfTwoNumbersInAnArray
{
public int FindMaximumXOR(int[] nums)
{
var set = new HashSet<int>(nums.Length);
int ret = 0, mask = 0;
for (int i = 31; i >= 0; --i)
{
mask |= 1 << i;
foreach (int num in nums)
set.Add(num & mask);
int find = ret | (1 << i);
foreach (int num in set)
{
if (set.Contains(num ^ find))
{
ret = find;
break;
}
}
set.Clear();
}
return ret;
}
}
}