leetcode 4. Median of Two Sorted Arrays
题目描述
https://leetcode.com/problems/median-of-two-sorted-arrays/
解决方法
一:
class Solution(object):
def findMedianSortedArrays(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: float
"""
nums3 = []
i = 0
j = 0
end = False
if len(nums1) == 0 or len(nums2) == 0:
end = True
while not end:
if nums1[i] > nums2[j]:
nums3.append(nums2[j])
if j < len(nums2)-1:
j = j + 1
elif j == len(nums2) -1:
j = j + 1
end = True
else:
end = True
else:
nums3.append(nums1[i])
if i < len(nums1)-1:
i = i + 1
elif i == len(nums1) -1:
i = i + 1
end = True
else:
end = True
if i >= len(nums1)-1 and j < len(nums2):
for j in range(j, len(nums2)):
nums3.append(nums2[j])
else:
for i in range(i, len(nums1)):
nums3.append(nums1[i])
lens = len(nums3)
# print(nums3)
# print(lens)
if lens % 2 == 0:
ans = nums3[int(lens/2)-1] + nums3[int(lens/2)]
# print(111)
return (ans/2.0)
else:
ans = nums3[int((lens+1)/2)-1]
return (ans/1)