class Solution(object):
def findMedianSortedArrays(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: float
"""
l1 = len(nums1)
l2 = len(nums2)
max_length = (l1 + l2) / 2
i = 0
l = []
index_1 = 0
index_2 = 0
while i <= max_length:
if index_1 >= l1:
l.append(nums2[index_2])
index_2+=1
elif index_2 >= l2:
l.append(nums1[index_1])
index_1 +=1
elif nums1[index_1] <= nums2[index_2] :
l.append(nums1[index_1])
index_1 +=1
else :
l.append(nums2[index_2])
index_2+=1
i += 1
if (l1 + l2) % 2 != 0:
return l[max_length]
return float(l[max_length] + l[max_length - 1]) / 2
if __name__ == '__main__':
test = Solution()
print(test.findMedianSortedArrays([1,3],[2]))
函数式leetcode-4
最新推荐文章于 2024-07-12 16:16:27 发布