LeetCode 4. Median of Two Sorted Arrays & 归并排序
Median of Two Sorted Arrays
搜索时间复杂度的时候,看到归并排序比较适合这个题目。中位数直接取即可,所以重点是排序。
再来看看治阶段,我们需要将两个已经有序的子序列合并成一个有序序列
第1次提交
class Solution:
def findMedianSortedArrays(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: float
"""
# merge sort
#print("start merge sort")
l1=len(nums1)
l2=len(nums2)
# median size
if (l1+l2)%2==0:
#length==4 4/2=2, [1,2] is index
median1=(l1+l2)//2-1
median2=(l1+l2)//2
else:
# length==3 3//2=1, [1,1] is index
median1=(l1+l2)//2
median2=median1
mergeList=[]
# two arrays' cursor
i=0
j=0
# median flag
medianFlag=False
while i<l1:
try:
if nums1[i]<=nums2[j]:
mergeList.append(nums1[i])
i+=1
else:
mergeList.append(nums2[j])
j+=1
# find size
if len(mergeList)>median2:
break
except:
break
# continue surplus, except find
while i<l1 and not medianFlag:
mergeList.append(nums1[i])
i+=1
while j<l2 and not medianFlag:
mergeList.append(nums2[j])
j+=1
#print(" median : ",median1,median2)
return (mergeList[median1]+mergeList[median2])/2
if __name__ == "__main__":
nums1 = [1, 3]
nums2 = [2]
data = [
{
'n1':[1, 3],
'n2':[2],
'result':2.0
},
{
'n1':[1, 2],
'n2':[3, 4],
'result':2.5
}
];
for d in data:
print(d)
result=Solution().findMedianSortedArrays(d['n1'],d['n2'])
print(result)
if result==d['result']:
print("--- ok ---")
else:
print("--- error ---")
哇,一次成功不带调试感觉很美妙,(hard也没那么难哈) 多亏那个博主图画的好,看个图就很明白了。很清晰。
总结:归并排序感觉实用性不是很强,不知道经常应用在什么情境下,有了分解才是再分治才是排序?