题目链接:https://leetcode-cn.com/problems/median-of-two-sorted-arrays/submissions/
class Solution:
def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float:
i = 0
j = 0
resultList = []
testLength = len(nums1) + len(nums2)
while i < len(nums1) and j < len(nums2) and len(resultList) <= testLength // 2:
if nums1[i] <= nums2[j]:
resultList.append(nums1[i])
i = i + 1
else:
resultList.append(nums2[j])
j = j + 1
while i < len(nums1) and len(resultList) <= testLength // 2:
resultList.append(nums1[i])
i = i + 1
while j < len(nums2) and len(resultList) <= testLength // 2:
resultList.append(nums2[j])
j = j + 1
if testLength % 2 == 0:
return (resultList[-2] + resultList[-1]) / 2
else:
return resultList[-1]
执行用时 :60 ms, 在所有 Python3 提交中击败了65.28%的用户
内存消耗 :13.6 MB, 在所有 Python3 提交中击败了6.15%的用户