• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

[Swift]LeetCode4. 两个排序数组的中位数 | Median of Two Sorted Arrays

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/9858777.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

There are two sorted arrays nums1 and nums2 of size m and n respectively.

Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).

You may assume nums1 and nums2 cannot be both empty.

Example 1:

nums1 = [1, 3]
nums2 = [2]

The median is 2.0

Example 2:

nums1 = [1, 2]
nums2 = [3, 4]

The median is (2 + 3)/2 = 2.5

给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 。

请找出这两个有序数组的中位数。要求算法的时间复杂度为 O(log (m+n)) 。

你可以假设 nums1 和 nums2 不同时为空。

示例 1:

nums1 = [1, 3]
nums2 = [2]

中位数是 2.0

示例 2:

nums1 = [1, 2]
nums2 = [3, 4]

中位数是 (2 + 3)/2 = 2.5

40ms
 1 lass Solution {
 2 func findMedianSortedArrays(_ nums1: [Int], _ nums2: [Int]) -> Double {
 3     var i = 0, j = 0
 4     var m = nums1.count, n = nums2.count
 5     var arr1 = nums1, arr2 = nums2
 6     if m > n {
 7         swap(&m, &n)
 8         swap(&arr1, &arr2)
 9     }
10     let half = (m + n) / 2
11 
12     var leftMax = 0, rightMin = 0
13     
14     // 长度为m的数组分割,有m+1种分割方法,所以 0 <= i <= m
15     var min = 0, max = m
16     while min <= max {
17         i = (min + max) / 2
18         j = half - i
19         if i > 0 && arr1[i-1] > arr2[j] {
20             max = i - 1
21         } else if i < m && arr2[j-1] > arr1[i] {
22             min = i + 1
23         } else {
24             // arr1 所有元素都在左边,则min(Right) = arr2[j]
25             if i == m {
26                 rightMin = arr2[j]
27             } else if j == n {
28                 rightMin = arr1[i]
29             } else {
30                 rightMin = arr1[i] > arr2[j] ? arr2[j] : arr1[i]
31             }
32             
33             if (m + n) % 2 == 1 {
34                 return Double(rightMin)
35             }
36             
37             if i == 0 {
38                 leftMax = arr2[j - 1]
39             } else if j == 0 {
40                 leftMax = arr1[i - 1]
41             } else {
42                 leftMax = arr1[i-1] > arr2[j-1] ? arr1[i-1] : arr2[j-1]
43             }
44             return Double(leftMax + rightMin) / 2.0
45         }
46     }
47     return 0
48 }
49 }

44ms

 1 class Solution {
 2 func findMedianSortedArrays(_ nums1: [Int], _ nums2: [Int]) -> Double {
 3         var A = nums1
 4         var B = nums2
 5         if A.count > B.count {
 6             B = nums1
 7             A = nums2
 8         }
 9         let m = A.count, n = B.count
10         var iMin = 0, iMax = m
11         while iMin <= iMax {
12             var i = 0, j = 0
13             if (iMin + iMax)%2 == 0 {
14                 i = (iMin + iMax) / 2
15             } else {
16                 i = (iMin + iMax - 1) / 2
17             }
18             
19             if (m+n)%2 == 0 {
20                 j = (m + n)/2 - i
21             } else {
22                 j = (m + n + 1)/2 - i
23             }
24             
25             if i < iMax && A[i] < B[j-1]{
26                 iMin = i + 1
27             } else if i > iMin && B[j] < A[i-1]{
28                 iMax = i-1
29             } else {
30                 var maxLeft = 0
31                 var minRight = 0
32                 if i == 0 {
33                     maxLeft = B[j-1]
34                 } else if j == 0 {
35                     maxLeft = A[i-1]
36                 } else {
37                     maxLeft = max(B[j-1], A[i-1])
38                 }
39                 
40                 if i == m {
41                     if j < n {
42                         minRight = B[j]
43                     }
44                 } else if j == n {
45                     if i < m {
46                         minRight = A[i]
47                     }
48                 } else {
49                     minRight = min(B[j],A[i])
50                 }
51                 
52                 if (A.count + B.count)%2 == 0 {
53                     return Double(minRight+maxLeft) / 2
54                 } else {
55                     return Double(maxLeft)
56                 }
57             }
58         }
59         return 0.0
60     }
61 }

48ms

 1 class Solution {
 2     func findMedianSortedArrays(_ nums1: [Int], _ nums2: [Int]) -> Double {
 3         if nums1.count == 0 {
 4             return medianOfSortedArray(nums2)
 5         } else if nums2.count == 0 {
 6             return medianOfSortedArray(nums1)
 7         }
 8         
 9         var index1: Int = 0
10         var index2: Int = 0
11         var sortedCombinedArray: [Int] = []
12         while index1 < nums1.count && index2 < nums2.count  {
13             if nums1[index1] < nums2[index2] {
14                 sortedCombinedArray.append(nums1[index1])
15                 index1 += 1
16             } else {
17                 sortedCombinedArray.append(nums2[index2])
18                 index2 += 1
19             }
20         }
21         
22         if index1 == nums1.count {
23             sortedCombinedArray += nums2[index2...]
24         } else {
25             sortedCombinedArray += nums1[index1...]
26         }
27         
28         return self.medianOfSortedArray(sortedCombinedArray)
29     }
30     
31     func medianOfSortedArray(_ sortedArray: [Int]) -> Double {  
32         if sortedArray.count == 1 {
33             return Double(sortedArray[0])
34         }
35         
36         if sortedArray.count % 2 == 1 {
37             let medianIndex = Int(sortedArray.count / 2)
38             return Double(sortedArray[medianIndex])
39         } else {
40             let medianIndex = Int(sortedArray.count / 2)
41             return Double(sortedArray[medianIndex] + sortedArray[medianIndex-1]) / 2.0
42         }
43     }
44 }   

48ms

 1 class Solution {
 2     func findMedianSortedArrays(_ nums1: [Int], _ nums2: [Int]) -> Double {
 3         var arr1 = nums1,arr2 = nums2
 4         if arr1.count > arr2.count {
 5             arr1 = nums2
 6             arr2 = nums1
 7         }
 8         var iMin = 0,iMax = arr1.count,halfLen = (arr1.count + arr2.count + 1)/2
 9         while iMin <= iMax {
10             let i = (iMin + iMax)/2
11             let j = halfLen - i
12             if i < iMax && arr2[j - 1] > arr1[i] {
13                 iMin = i + 1
14             }else if i > iMin && arr1[i - 1] > arr2[j] {
15                 iMax = i - 1
16             }else {
17                 var maxLeft = 0
18                 if i == 0 {
19                     maxLeft = arr2[j - 1]
20                 }else if j == 0 {
21                     maxLeft = arr1[i - 1]
22                 }else{
23                     maxLeft = max(arr1[i - 1], arr2[j - 1])
24                 }
25 
26                 if (arr1.count + arr2.count)%2 == 1 {
27                     return Double(maxLeft)
28                 }
29                 var maxRight = 0
30                 if i == arr1.count {
31                     maxRight = arr2[j]
32                 }else if j == arr2.count {
33                     maxRight = arr1[i]
34                 }else{
35                     maxRight = min(arr2[j], arr1[i])
36                 }
37                 return (Double(maxLeft) + Double(maxRight))/2
38             }
39         }
40         return 0
41     }
42 }

52ms

 1 class Solution {
 2     func findMedianSortedArrays(_ nums1: [Int], _ nums2: [Int]) -> Double {
 3         let total = nums1.count + nums2.count
 4         if total % 2 == 1 {
 5             return findKth(nums1, nums1.count, nums2, nums2.count, total / 2 + 1)
 6         } else {
 7             return (findKth(nums1, nums1.count, nums2, nums2.count, total / 2) + findKth(nums1, nums1.count, nums2, nums2.count, total / 2 + 1)) / 2
 8         }
 9     }
10     
11     private func findKth(_ nums1: [Int], _ m: Int, _ nums2: [Int], _ n: Int, _ k: Int) -> Double {
12         if m > n {
13             return findKth(nums2, n, nums1, m, k)
14         }
15         
16         if m == 0 {
17             return Double(nums2[k - 1])
18         }
19         
20         if k == 1 {
21             return Double(min(nums1[0], nums2[0]))
22         }
23         
24         let i = min(k / 2, m)
25         let j = k - i
26         if nums1[i - 1] < nums2[j - 1] {
27             return findKth(Array(nums1[i..<m]), m - i, nums2, n, k - i)
28         } else if nums1[i - 1] > nums2[j - 1] {
29             return findKth(nums1, m, Array(nums2[j..<n]), n-j, k - j)
30         } else {
31             return Double(nums1[i - 1])
32         }
33     }
34 }

60ms

 1 class Solution {
 2     func findMedianSortedArrays(_ nums1: [Int], _ nums2: [Int]) -> Double {
 3         var nums = nums1 + nums2
 4         nums.sort(by: <)
 5         
 6         let size = nums.count
 7         let mid: Int = size / 2
 8         if size % 2 == 0 {
 9             return Double((nums[mid - 1] + nums[mid])) / 2
10         } else {
11             return Double(nums[mid])
12         }
13     }
14 }

64ms

 1 class Solution {
 2     func findMedianSortedArrays(_ nums1: [Int], _ nums2: [Int]) -> Double {
 3         let combinedNums = nums1 + nums2
 4         let sortedCombinedNums = combinedNums.sorted { $0 > $1 }
 5         if sortedCombinedNums.count % 2 == 0 { // even
 6             let count = sortedCombinedNums.count
 7             let middleIndex2 = count / 2
 8             let middleIndex1 = middleIndex2 - 1
 9             let middleSum = sortedCombinedNums[middleIndex1] + sortedCombinedNums[middleIndex2]
10             let median = Double(middleSum) / 2
11             return median
12         } else { // odd
13             let middleIndex = (sortedCombinedNums.count / 2)
14             let median = Double(sortedCombinedNums[middleIndex])
15             return median
16         }
17     }
18 }

72ms

 1 class Solution {
 2     func findMedianSortedArrays(_ nums1: [Int], _ nums2: [Int]) -> Double {
 3         let merged = (nums1 + nums2).sorted(by: {return $0 < $1})
 4     let count = merged.count
 5     guard count > 0 else { return 0 }
 6     if count % 2 == 0 {
 7         let a = Double(merged[Int(count/2)])
 8         let b = Double(merged[Int(count/2) - 1])
 9         return (a + b) / 2
10         
11     } else {
12         return Double(merged[Int(count-1)/2])
13         
14     }
15     }
16 }

84ms

 1 class Solution {
 2     func findMedianSortedArrays(_ nums1: [Int], _ nums2: [Int]) -> Double {
 3         var nums = nums1 + nums2
 4         nums.sort()
 5         if nums.count == 0 {
 6             return 0
 7         } else {
 8             if nums.count % 2 == 0 {
 9                 return (Double(nums[nums.count/2]) + Double(nums[nums.count/2-1]))/2
10             } else {
11                 return Double(nums[nums.count/2])
12             }
13         }
14     }
15 }

88ms

 1 class Solution {
 2     func findMedianSortedArrays(_ nums1: [Int], _ nums2: [Int]) -> Double {
 3         var totalArray = nums1 + nums2 
 4         var resultArray = totalArray.sorted()
 5         
 6         if resultArray.count % 2 == 1 {
 7             var midPoint = (resultArray.count - 1) / 2
 8             return Double(resultArray[midPoint])
 9 
10         } else {
11             var left = resultArray.count / 2 - 1
12             var right = resultArray.count / 2
13             print(right)
14             return Double((Double(resultArray[left]) + Double(resultArray[right])) / 2)
15         }
16     }
17 }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Swift高级运算符发布时间:2022-07-13
下一篇:
OC与Swift混编发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap