您的位置:首页 > 其它

LeetCode#4 Median of Two Sorted Arrays

2015-07-04 07:21 337 查看
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)).

class Solution:
# @param {integer[]} nums1
# @param {integer[]} nums2
# @return {float}
def findMedianSortedArrays(self, nums1, nums2):
i = 0
j = 0
k = 0
newArr = []

while i < len(nums1) and j < len(nums2):
if nums1[i]<=nums2[j]:
newArr.append(nums1[i])
i+=1
k+=1
else:
newArr.append(nums2[j])
j+=1
k+=1

while i < len(nums1):
newArr.append(nums1[i])
i+=1
k+=1
while j < len(nums2):
newArr.append(nums2[j])
j+=1
k+=1

num = len(nums1) + len(nums2)

if num % 2 == 1:
return newArr[num/2]
else:
return float(newArr[num/2-1]+newArr[num/2])/2

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: