美文网首页
421. Maximum XOR of Two Numbers

421. Maximum XOR of Two Numbers

作者: 阿团相信梦想都能实现 | 来源:发表于2017-01-01 03:13 被阅读0次
    class Solution(object):
        def findMaximumXOR(self, nums):
            """
            :type nums: List[int]
            :rtype: int
            """
            max,mask=0,0
            
            for i in range(31,-1,-1): 
                mask|=(1<<i)
                prefix_set=set()
                for num in nums:
                    prefix_set.add(num&mask)
                temp=max|(1<<i)
                for prefix in prefix_set:
                    if temp^prefix in prefix_set:
                        max=temp
                        break
            return max
            
    
    class Solution(object):
        def findMaximumXOR(self, nums):
            """
            :type nums: List[int]
            :rtype: int
            """
            answer=0
            for i in range(31,-1,-1):
                answer<<=1
                prefixes={num>>i for num in nums}
                answer+=any(answer^1^prefix in prefixes for prefix in prefixes)
            return answer
                     
                 
                
    

    相关文章

      网友评论

          本文标题:421. Maximum XOR of Two Numbers

          本文链接:https://www.haomeiwen.com/subject/xwgbvttx.html