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.
  1. class Solution:
  2. def findMaximumXOR(self, nums):
  3. """
  4. :type nums: List[int]
  5. :rtype: int
  6. """
  7. head = self.buildTrie(nums)
  8. maxXor = 0
  9. for num in nums:
  10. node = head
  11. curXor = 0
  12. for bit in range(31, -1, -1):
  13. chd = int(bool(num & (1 << bit)))
  14. if node[1 ^ chd]:
  15. curXor |= 1 << bit
  16. node = node[1 ^ chd]
  17. else:
  18. node = node[chd]
  19. maxXor = max(maxXor, curXor)
  20. return maxXor
  21. def buildTrie(self, nums):
  22. root = [None, None]
  23. for num in nums:
  24. cur = ‘{0:b}‘.format(num).zfill(32)
  25. node = root
  26. for i in cur:
  27. bit = int(i)
  28. if not node[bit]:
  29. node[bit] = [None, None]
  30. node = node[bit]
  31. return root
  32. nums = [3, 10, 5, 25, 2, 8]
  33. s = Solution()
  34. res = s.findMaximumXOR(nums)
  35. print(res)

来自为知笔记(Wiz)

原文地址:https://www.cnblogs.com/xiejunzhao/p/8331683.html

时间: 2024-10-30 02:04:23

421. Maximum XOR of Two Numbers in an Array 数组中两个数的最大异或的相关文章

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] 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

[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

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

【ShareCode】不错的技术文章 -- 如何使用异或(XOR)运算找到数组中缺失的数?

如何使用异或(XOR)运算找到数组中缺失的数? 今天给大家分享一篇关于使用XOR(异或)运算找到数组中缺失的数的问题. 在一次Javascript面试中,有这么一个问题: 假设有一个由0到99(包含99)的整数组成的长度为100的数组.从数组中随机移除一个元素,得到了一个长度为99的数组,那么请问如何找到所取出的数字是几?(假设数组未排序). 大多数面试者都是按照如下方法解答的: 首先对数组进行排序,然后遍历一遍数组,检查数组中相邻两项的的差,如果差大于1,则找到缺失的数字. 这是一种有效的算法

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