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

posted on 2017-05-23 10:30  Sempron2800+  阅读(196)  评论(0编辑  收藏  举报