题目描述很简单,就是找到两个有序数组合并后的中位数,要求时间复杂度O(log (m+n))。
如果不要去时间复杂度,很容易就想到了归并排序,归并排序的时间复杂度是O(m+n),空间复杂度也是O(m+n),不满足题目要求,其实我开始也不知道怎么做,后来看了别人的博客才知道有个二分法求两个有序数组中第k大数的方法。
代码如下:
题目描述很简单,就是找到两个有序数组合并后的中位数,要求时间复杂度O(log (m+n))。
如果不要去时间复杂度,很容易就想到了归并排序,归并排序的时间复杂度是O(m+n),空间复杂度也是O(m+n),不满足题目要求,其实我开始也不知道怎么做,后来看了别人的博客才知道有个二分法求两个有序数组中第k大数的方法。
代码如下:
双链表基本运算的代码实现,前插、后插操作
原题链接在这里:https://leetcode.com/problems/median-of-two-sorted-arrays/ 题目: here are two sorted arrays nums1 and nums2 of size m and n respectively. Find t
2017/2/5更新:如果一定要每次扔一半,使得时间复杂度为O(log(m+n))。可以在第一个数组找前k1个,第二个数组找前k2个,使得k1+k2 == k, 分情况: 1. if A[k1] < B[k2], then A[k1]及之前的、B[k2+1]及之后的都不可能成为第k个元素,所以扔掉
举报文章
请选择举报类型
补充说明
0/200
上传截图
格式支持JPEG/PNG/JPG,图片不超过1.9M