Skip to content

Commit 4bdbbcb

Browse files
author
liningkang
committed
Update solution/004.Median of Two Sorted Arrays/Solution.swift
1 parent 005a0c7 commit 4bdbbcb

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
func findMedianSortedArrays(_ nums1: [Int], _ nums2: [Int]) -> Double {
3+
4+
var i = 0, j = 0
5+
6+
var firstValue = 0, secondValue = 0
7+
8+
for index in 0..<nums1.count + nums2.count {
9+
10+
var numsIndexValue1 = 0, numsIndexValue2 = 0
11+
12+
(nums1.count > 0 && i < nums1.count) ? (numsIndexValue1 = nums1[i]) : (numsIndexValue1 = Int.max)
13+
(nums2.count > 0 && j < nums2.count) ? (numsIndexValue2 = nums2[j]) : (numsIndexValue2 = Int.max)
14+
15+
numsIndexValue1 > numsIndexValue2 ? (j = j + 1) : (i = i + 1)
16+
17+
if index == ((nums1.count + nums2.count + 1)/2 - 1) {
18+
firstValue = min(numsIndexValue1, numsIndexValue2)
19+
}
20+
if index == ((nums1.count + nums2.count + 2)/2 - 1) {
21+
secondValue = min(numsIndexValue1, numsIndexValue2)
22+
}
23+
}
24+
25+
return Double(firstValue + secondValue) / 2
26+
}
27+
}

0 commit comments

Comments
 (0)