# 给定两个大小为 m 和 n 的有序数组 nums1 和 nums2
# (1) 合并有序数组并排序(不能用sort方法)
# (2) 请你找出这两个有序数组的中位数 (注:需要考虑两个数组为空的情况)
'''
思路:
1、将两个数组合并到一个数组中
2、进行冒泡排序
'''
class Solution:
def findMedianSortedArrays(self, nums1: list, nums2: list) -> float:
temp=[]
for i in nums1:
temp.append(i)
for j in nums2:
temp.append(j)
for m in range(len(temp)-1):
for n in range(len(temp)-1-m):
if temp[n]>temp[n+1]:
temp[n],temp[n+1]=temp[n+1],temp[n]
length = len(temp)
if length == 0:
return '两个表均为空,没有中位数'
elif length % 2 == 1:
return temp[length // 2]
else:
return (temp[length // 2] + temp[length // 2 - 1]) / 2
print(Solution().findMedianSortedArrays([],[]))
print(Solution().findMedianSortedArrays([],[-3,-2,5,7]))
print(Solution().findMedianSortedArrays([1,2,3],[]))
print(Solution().findMedianSortedArrays([1,2,3],[-4,-5,-6]))
执行结果:
两个表均为空,没有中位数
1.5
2
-1.5



