Median of two sorted arrays - leetcode

Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays.

The overall run time complexity should be O(log (m+n)).

 

Example 1:

Input: nums1 = [1,3], nums2 = [2]
Output: 2.00000
Explanation: merged array = [1,2,3] and median is 2.

Example 2:

Input: nums1 = [1,2], nums2 = [3,4]
Output: 2.50000
Explanation: merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5.

The outcome:


Runtime:25ms

Memory:16.22MB

My code:

class Solution {

func findMedianSortedArrays(_ nums1: [Int], _ nums2: [Int]) -> Double {

var nums3: [Int] = nums1+nums2

nums3.sort()

var count: Int = nums3.count

if count % 2 == 1 {

return Double(nums3[count / 2])

} else {

return (Double(nums3[count / 2 - 1]) + Double(nums3[count / 2])) / 2.0

}

}

}

Next
Next

Processing a “locale” file with Python