python刷LeetCode 4. 寻找两个正序数组的中位数 暴力挑战hard
题目要求时间复杂度log n,但我一开始只想到二分排序,所以就nlogn提交试一下,居然通过了,不过排名垫底就是了。。。。。。
然后分享一下代码:
def sort(li,first,last):
if first>=last:
return
low,high=first,last
pivot=li[first]
while low<high:
while low<high and pivot<=li[high]:
high-=1
li[low]=li[high]
while low<high and pivot>li[low]:
low+=1
li[high]=li[low]
li[low]=pivot
sort(li,low+1,last)
sort(li,first,low-1)
class Solution:
def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float:
nums1.extend(nums2)
print(nums1)
l=len(nums1)
sort(nums1,0,l-1)
print(nums1)
if l%2 == 0:
ans=(nums1[int(l/2)]+nums1[int(l/2-1)])/2
# return ans
else:
# return nums1[l//2]
ans=nums1[l//2]
# return ans
return ans