Swift-Median of Two Sorted Arrays
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)).
Example 1:
nums1 = [1, 3]
nums2 = [2]
The median is 2.0`</code></pre>
Example 2:
<pre><code>`nums1 = [1, 2]
nums2 = [3, 4]
The median is (2 + 3)/2 = 2.5
如果不考慮時間複雜度,最簡單就是將兩個陣列合並,然後尋找中位數:
func findMedianSortedArrays(_ nums1: [Int], _ nums2: [Int]) -> Double {
var index1:Int = 0
var index2:Int = 0
var preNum = 0
var currentNum = 0
var count:Int = 0
let count1 = nums1.count
let count2 = nums2.count
let midIndex = (count1 + count2) / 2
while index1 < count1 && index2 < count2 {
if count - 1 == midIndex {
break
}
preNum = currentNum
if nums1[index1] < nums2[index2] {
currentNum = nums1[index1]
index1 += 1
} else {
currentNum = nums2[index2]
index2 += 1
}
count += 1
}
while index1 < count1 {
if count - 1 == midIndex {
break
}
preNum = currentNum
currentNum = nums1[index1]
index1 += 1
count += 1
}
while index2 < count2 {
if count - 1 == midIndex {
break
}
preNum = currentNum
currentNum = nums2[index2]
index2 += 1
count += 1
}
var result:Double = 0
if (count1 + count2) % 2 == 0 {
result = Double(preNum + currentNum) / 2.0
} else {
result = Double(currentNum)
}
return result
}
按照題目要求的複雜度,可以通過二分法遞迴實現,找兩個已排序陣列的中位數,其實就是將兩個有序陣列有序合併後找第K小的數。而找第K小的數,可以將K平分到兩個陣列中,然後利用一個重要的結論:如果A[k/2-1]<B[k/2-1],那麼A[0]~A[k/2-1]一定在第k小的數的序列當中.
func findMedianSortedArrays3(_ nums1: [Int], _ nums2: [Int]) -> Double {
let count = nums1.count + nums2.count
if count % 2 == 1 {
return findKth(num1: num1, m: num1.count, num2: num2, n: num2.count, k: count / 2 + 1)
} else {
let mid1:Double = findKth(num1: num1, m: num1.count, num2: num2, n: num2.count, k: count / 2 + 1)
let mid2:Double = findKth(num1: num1, m: num1.count, num2: num2, n: num2.count, k: count / 2)
return (mid1 + mid2) / 2
}
}
func findKth(num1:[Int], m:Int, num2:[Int], n:Int, k:Int) -> Double {
// 假定第一個陣列為較小陣列
if m > n {
return findKth(num1: num2, m: n, num2: num1, n: m, k: k)
}
if m == 0 {
return Double(num2[k - 1])
}
if k == 1 {
return Double(min(num1[0], num2[0]))
}
let lenA = min(k / 2, m)
let lenB = k - lenA
if num1[lenA - 1] < num2[lenB - 1] {
let temp:[Int] = num1
return findKth(num1: Array(temp.suffix(from: lenA)), m: m - lenA, num2: num2, n: n, k: k - lenA)
} else if num1[lenA - 1] > num2[lenB - 1] {
let temp:[Int] = num2
return findKth(num1: num1, m: m, num2:Array(temp.suffix(from: lenB)) , n: n - lenB, k: k - lenB)
} else {
return Double(num2[lenA - 1])
}
}
- 保持num1是短的那一個陣列,num2是長的
- 平分k, 一半在num1,一半在num2 (如果num1的長度不足K/2,那就pa就指到最後一個)
- 如果lenA的值 < lenB的值,那證明第K個數肯定不會出現在pa之前,遞迴,把num1陣列pa之前的資料刪除.同理遞迴砍B陣列.
- 遞迴到 m == 0(num1為空)返回 B[k - 1], 或者k == 1(找第一個數)就返回min(num1第一個數num2第一個數)
相關文章
- Leetcode Median of Two Sorted ArraysLeetCode
- Leetcode 4 Median of Two Sorted ArraysLeetCode
- Leetcode-Median of Two Sorted ArraysLeetCode
- LeetCode 4. Median of Two Sorted ArraysLeetCode
- LeetCode2:Median of Two Sorted ArraysLeetCode
- LeetCode Median of Two Sorted Arrays(004)解法總結LeetCode
- [LeetCode] Median of Two Sorted Arrays 兩個有序陣列的中位數LeetCode陣列
- LeetCode | 349 Intersection Of Two ArraysLeetCode
- CodeForces 1417B Two Arrays
- Leetcode Merge Two Sorted ListsLeetCode
- leetcode 4. Median of Two Sorted Arrays 尋找兩個正序陣列的中位數(困難)LeetCode陣列
- leetcode 350. Intersection of Two Arrays IILeetCode
- 349. Intersection of Two Arrays--LeetCode RecordLeetCode
- Leetcode 21 Merge Two Sorted ListsLeetCode
- Leetcode-Merge Two Sorted ListsLeetCode
- Median of Two Sorted Array leetcode javaLeetCodeJava
- Merge Two Sorted Lists leetcode javaLeetCodeJava
- LeetCode 21. Merge Two Sorted ListsLeetCode
- leetcode21_Merge Two Sorted ListsLeetCode
- 350. Intersection of Two Arrays II--LeetCode RecordLeetCode
- [LeetCode] 1385. Find the Distance Value Between Two ArraysLeetCode
- LeetCode 第 21 題 (Merge Two Sorted Lists)LeetCode
- 【Leetcode】167. Two Sum II - Input array is sortedLeetCode
- LeetCode Merge Two Sorted Lists(021)解法總結LeetCode
- CF 773 (Div. 1) D. Two Arrays 雙指標 容斥指標
- [LintCode] Merge Two Sorted Lists 混合插入有序連結串列
- [LeetCode] Merge Two Sorted Lists 混合插入有序連結串列LeetCode
- 資料結構與演算法 | Leetcode 21:Merge Two Sorted Lists資料結構演算法LeetCode
- Arrays
- delphi open arrays和dynamic arrays區別
- 六,Arrays
- Arrays Basics
- 【java】Arrays類Java
- Arrays工具類
- coca after two months vs in two months
- Java中Arrays作用Java
- Java Arrays.sort()Java
- java Arrays陣列Java陣列