#4. Median of Two Sorted Arrays

上一篇 / 下一篇  2017-08-02 00:55:33 / 个人分类:leetcode

class Solution(object):
# There are two sorted arrays nums1 and nums2 of size m and n respectively.
#
# Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).
#
# Example 1:
# nums1 = [1, 3]
# nums2 = [2]
#
# The median is 2.0
# Example 2:
# nums1 = [1, 2]
# nums2 = [3, 4]
#
# The median is (2 + 3)/2 = 2.5
def findMedianSortedArrays(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: float
"""
tmp_list = list()
tmp_list.extend(nums1)
tmp_list.extend(nums2)
tmp_list.sort()
if len(tmp_list)%2:
mid = len(tmp_list)//2
return float(tmp_list[mid])
else:
mid1 = len(tmp_list)//2-1
mid2 = len(tmp_list)//2
result = (tmp_list[mid1]+tmp_list[mid2])/2.0
return result

a = Solution()
print(a.findMedianSortedArrays([1,2],[3,4]))

TAG:

 

评分:0

我来说两句

显示全部

:loveliness: :handshake :victory: :funk: :time: :kiss: :call: :hug: :lol :'( :Q :L ;P :$ :P :o :@ :D :( :)

Open Toolbar