File tree 1 file changed +27
-0
lines changed
solution/004.Median of Two Sorted Arrays
1 file changed +27
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments