Median of Two Sorted Arrays
题目难度:hard
题目要求:
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)).
You may assume nums1 and nums2 cannot be both empty.
翻译:有两个长度分别为m和n的有序数组,分别为nums1和nums2,找到两个数组的中位数,时间复杂度要求为O(log(m+n))。num1和nums2不为空。
菜鸡解法:
我的做法比较复杂,但通过了。
Runtime: 104 ms, faster than 65.11% of Python3 online submissions for
Median of Two Sorted Arrays.
大概思路是,既然两个数组大小已知,那么我们就可以根据两个有序数组有序合并之后(记为N)的大小判断出中位数应当由第几个数来得出。因此设置i和j逐个比较两个数组元素大小,并用size记录合并数组N中已经确定的元素个数,当size递增到中位数的位置时,即可表示出中位数。<