[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 ≤ ij < 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.


给定一个非空数组,数组中元素为 a0, a1, a2, … , an-1,其中 0 ≤ ai < 231 。

找到 ai 和aj 最大的异或 (XOR) 运算结果,其中0 ≤ i,  j < 

你能在O(n)的时间解决这个问题吗?

示例:

输入: [3, 10, 5, 25, 2, 8]

输出: 28

解释: 最大的结果是 5 ^ 25 = 28.

196ms
 1 class Solution {
 2     func findMaximumXOR(_ nums: [Int]) -> Int {
 3         var res:Int = 0
 4         var mask:Int = 0
 5         for i in (0...31).reversed()
 6         {
 7             mask |= (1 << i)
 8             var s:Set<Int> = Set<Int>()
 9             for num in nums
10             {
11                 s.insert(num & mask)
12             }
13             var t:Int = res | (1 << i)
14             for prefix in s
15             {
16                 if s.contains(t ^ prefix)
17                 {
18                     res = t
19                     break
20                 }
21             }
22         }
23         return res
24     }
25 }


872ms

 1 class TrieNode {
 2     var children = [Int: TrieNode]()
 3     var end: Bool
 4     var value: Int = -1
 5
 6     init() {
 7         end = false
 8     }
 9
10     func hasChild(_ key: Int) -> Bool {
11         return children[key] != nil
12     }
13
14     func makeChild(_ key: Int) {
15         children[key] = TrieNode()
16     }
17
18     func getChild(_ key: Int) -> TrieNode {
19         return children[key]!
20     }
21 }
22
23
24 class Solution {
25
26     let INT_SIZE = 32
27     var root = TrieNode()
28
29     func findMaximumXOR(_ nums: [Int]) -> Int {
30
31         var ans = 0
32         insert(nums[0])
33
34         for i in 1..<nums.count {
35             ans = max(ans, query(nums[i]))
36             insert(nums[i])
37         }
38
39         return ans
40
41     }
42
43     func query(_ key: Int) -> Int {
44         var current = root
45
46         for i in (0..<INT_SIZE).reversed() {
47             let current_bit = (key & 1<<i) >= 1 ? 1 : 0
48             let next: TrieNode?
49             if current.hasChild(1-current_bit) {
50                 next = current.getChild(1-current_bit)
51             } else {
52                 next = current.getChild(current_bit)
53             }
54
55             current = next!
56         }
57
58         return key ^ current.value
59
60     }
61
62     func insert(_ num: Int) {
63
64         var current = root
65
66         for i in (0..<INT_SIZE).reversed() {
67             let key = (num & 1<<i) >= 1 ? 1 : 0
68             if !current.hasChild(key) {
69                 current.makeChild(key)
70             }
71
72             current = current.getChild(key)
73         }
74
75         current.end = true
76         current.value = num
77     }
78 }

原文地址:https://www.cnblogs.com/strengthen/p/10333729.html

时间: 2024-08-03 11:41:00

[Swift]LeetCode421. 数组中两个数的最大异或值 | 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-数组中两个数的最大异或值*(前缀树)

题目描述: 方法一: class Solution: def findMaximumXOR(self, nums: List[int]) -> int: root = TreeNode(-1) for num in nums: cur_node = root #当前的node for i in range(0, 32): #代表32个位 # print num, 1 <<(31 - i), num & (1 <<(31 - i)) if num & (1 &l

无序数组array, 找到数组中两个数的最大差值

题目链接: 无序数组array, 找到数组中两个数的最大差值, 且大数出现在小数之后,如:arr[i]-arr[j], 且 i<j.比如: array 是 [2, 3, 10, 6, 4, 8, 1],最大差值是8(10-2) 解题思路: 记录当前访问过的数组中的最小值 min_val; 2) 当前元素值arr[i] - min_val 和 max_diff作比较 若大于 max_diff , 则更新它的值 1 import javax.validation.constraints.Min; 2

有序数组中两个数的和等于一个输入值的函数

题目:        输入一个已经按升序排序过的数组和一个数字,在数组中查找两个数,使得它们的和正好是输入的那个数字.要求时间复杂度为O(N). 如果有多对数字的和等于输入的数字,输出任意一对即可. 例如输入数组1,2,4,7,11,15和数字15,由于4+11=15,因此输出4和11. 代码如下: /*data[] 为有序数组, length 为数组的长度 sum为用户输入的和 num1 为符合和等于sum的第一个数 num2 为第二个数*/ #include<iostream> using

Java之求数组中两个数之和

给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的 两个 整数. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组中同样的元素. 示例: 给定 nums = [2, 7, 11, 15], target = 9 因为 nums[0] + nums[1] = 2 + 7 = 9 所以返回 [0, 1] 通过使用空间换取时间,降低时间复杂度时间复杂度O(n)空间复杂度o(n) public int[] twoSum(int[] nums, int

经典算法学习——快速找出数组中两个数字,相加等于某特定值

这个算法题的描述如下:快速找出一个数组中的两个数字,让这两个数字之和等于一个给定的值.目前我假设数组中的都是各不相等的整数.这道题是我在一次面试中被问到的,由于各种原因,我没回答上来,十分尴尬.其实这道题十分简单,我们使用相对巧妙的方法来实现下.注意不使用两层循环的元素遍历.示例代码上传至:https://github.com/chenyufeng1991/SumTo100 . 算法描述如下: (0)首先对原数组进行排序,成为递增数组: (1)对排序后的数组头部i [0]和数组尾部j [n-1]

java语言插入数组中一个数,仍然能够实现排序

package com.llh.demo; import java.util.Scanner; /** * * @author llh * */ public class Demo16 { /* * 插入数组中一个数,仍能排序 */ public static void main(String[] args) { int []arr={100,70,50,30,10,0}; int []bb=new int[arr.length+1]; System.out.print("请输入你要插入的数字:

在已排序好的数组找两个数a加b等于给定的N

public class 在已排序好的数组找两个数a加b等于给定的N { public static void main(String[] args) { /** * 初始化参数 Result为结果值 * num 是测试数组 * start 开始游标, end 结束游标 */ int Result = 15; int[] num = {1,2,4,7,11,15}; int start = 0, end = num.length-1; //从数组的两端开始扫,若两数之和小于目标,则头往后进一位,

交换数组中两个元素的位置,元素包括key和value 一维数组

/*author: [email protected]description: 交换数组中两个元素的位置,元素包括key和value,具体用法见下面的例子*/$arr = array(11=>'a',22=>'b',33=>'c',44=>'d');$res = array_exchange($arr, 11 ,33); //example:echo '<pre>';print_r ($res);echo '</pre>'; function array_e