这道题目和PAT上的1029是同一题。但是PAT1029用O(m+n)的时间复杂度(题解)就可以,这道题要求是O(log(m+n))。

这道题花费了我一个工作日的时间来思考。因为是log因而一直思考如何进行二分,想着是两个vector分别二分,但一直想不到合适的解法,就是无穷尽的if else

后来看了题解。思路是从两个有序数组中寻找第k小的数。

<1>花了很长时间在调试上,结果发现是少了一个return,期间还不停怀疑自己写递归的能力。。。要被自己打败了,调代码真是一件细小却很耗费心神的事情。

<2>对vector的使用也是醉了。

思路如下:

  我们先假设nums1nums2数组中元素个数都是大于 k/2 的,且从 0 开始编号,那么我们比较 a = nums1[k/2 - 1] 和 b = nums2[k/2 - 1]。

  (1)如果 a < b 那么 nums1[0] 到 nums1[k/2 - 1] 这 k/2 个数在合并后的有序数组中,一定在第 k 小的数左边。为什么呢?

  我们发现,nums1数组中比 a 小的数一共是 k/2 - 1 个。nums2数组中比 a 小的数最多有 k/2 - 1 个。因而合并以后比 a 小的数最多有k/2 - 1 + k/2 - 1 < k - 2。

  也就是说 a 最多是第 k-1 小的数。所以说nums1数组前 k/2 个数可以剔除了。

  (2)如果 a > b 同理,剔除掉 nums2数组前 k/2 个数。

  (3)如果 a = b,a 即为所求。

  每次剔除掉 k 一半个元素,因而时间复杂度是O(logk),对于此题,k = (m+n)/2 所以时间复杂度是O(log(m+n))

考虑实际情况:

  如果nums1nums2数组中元素个数不足 k/2 个的话,一般情况下,我们只需要满足两者前面的总数目为 k 即可,原理和上述的原理是类似的,不再赘述。

  此外还需要考虑特殊情况,

  (1)一个数组为空,则放回另一个数组的第 k 大;

  (2)k==1则直接返回min(nums1[0],nums2[0])。

在实际实现的时候,由于输入是vector,它是一个动态数组,可以实时增加或删除元素,但是无法向普通数组/指针一样任意指定起始位置和结束为止,因而只好使用了删除操作

erase(iter1,iter2),删除[iter1,iter2)之间的元素。 

 class Solution {
public:
double findkth(vector<int>& nums1,vector<int>& nums2, int k)
{
int m = nums1.size(),n = nums2.size();
if(m > n)
return findkth(nums2,nums1,k);//error 1. forget the "return";
if(m == )
return double(nums2[k - ]);//error 2. write as nums2[n - 1];
if(k == )
{
return double(min(nums1[],nums2[]));
}
int pa = min(k / ,m), pb = k - pa;
if(nums1[pa - ] < nums2[pb - ])
{
nums1.erase(nums1.begin(),nums1.begin() + pa);
return findkth(nums1,nums2,k - pa);
}
else if(nums1[pa - ] > nums2[pb - ])
{
nums2.erase(nums2.begin(),nums2.begin() + pb);
return findkth(nums1,nums2,k - pb);
}
else
return double(nums1[pa - ]);
}
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
int len1 = nums1.size(),len2 = nums2.size();
int len = len1 + len2;
vector<int> cop1(nums1),cop2(nums2);
if(len % )
{
return findkth(nums1,nums2,len / + );
}
else
{
double t1=findkth(nums1,nums2,len / ),t2=findkth(cop1,cop2,len / + );
return (t1 + t2) / ;
}
}
};

删除vector的元素需要①额外备份一下数组,②删除操作。无谓的耗时,因而重新实现成手动标记数组的起始位置。只需要在上面实现方法的基础上 + star就可以了,原理都是一样的。

 class Solution {
public:
double findkth(vector<int>& nums1,int st1,int ed1,vector<int>& nums2, int st2,int ed2,int k)
{
int m = ed1 - st1,n = ed2 - st2;
if(m > n)
return findkth(nums2,st2,ed2,nums1,st1,ed1,k);//error 1. forget the "return";
if(m == )
return double(nums2[st2 + k - ]);//error 2. write as nums2[n - 1];
if(k == )
{
return double(min(nums1[st1],nums2[st2]));
}
int pa = min(k / ,m), pb = k - pa;
if(nums1[st1 + pa - ] < nums2[st2 + pb - ])
{
return findkth(nums1,st1 + pa,ed1,nums2,st2,ed2,k - pa);
}
else if(nums1[st1 + pa - ] > nums2[st2 + pb - ])//error 3. forget the "st2 +";
{
return findkth(nums1,st1,ed1,nums2,st2 + pb,ed2,k - pb);
}
else
return double(nums1[st1 + pa - ]);
}
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
int len1 = nums1.size(),len2 = nums2.size();
int len = len1 + len2;
if(len % )
{
return findkth(nums1,,len1,nums2,,len2,len / + );
}
else
{
double t1 = findkth(nums1,,len1,nums2,,len2,len / ),t2 = findkth(nums1,,len1,nums2,,len2,len / + );
return (t1 + t2) / ;
}
}
};

LeetCode4. Median of Two Sorted Arrays---vector实现O(log(m+n)--- findkth的更多相关文章

  1. LeetCode4 Median of Two Sorted Arrays

    题目: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...

  2. Leetcode4:Median of Two Sorted Arrays@Python

    There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...

  3. leetcode4 Median of Two Sorted Arrays学习记录

    学习了扁扁熊的题解:https://leetcode-cn.com/problems/median-of-two-sorted-arrays/solution/4-xun-zhao-liang-ge- ...

  4. Leetcode4.Median of Two Sorted Arrays两个排序数组的中位数

    给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 . 请找出这两个有序数组的中位数.要求算法的时间复杂度为 O(log (m+n)) . 你可以假设 nums1 和 nums2 不同 ...

  5. [LintCode] Median of Two Sorted Arrays 两个有序数组的中位数

    There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted ...

  6. leetcode-algorithms-4 Median of Two Sorted Arrays

    leetcode-algorithms-4 Median of Two Sorted Arrays There are two sorted arrays nums1 and nums2 of siz ...

  7. 【LeetCode】4. Median of Two Sorted Arrays (2 solutions)

    Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. Find t ...

  8. 《LeetBook》leetcode题解(4): Median of Two Sorted Arrays[H]——两个有序数组中值问题

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  9. Leetcode 4. Median of Two Sorted Arrays(二分)

    4. Median of Two Sorted Arrays 题目链接:https://leetcode.com/problems/median-of-two-sorted-arrays/ Descr ...

  10. 4. Median of Two Sorted Arrays(topK-logk)

    4. Median of Two Sorted Arrays 题目 There are two sorted arrays nums1 and nums2 of size m and n respec ...

随机推荐

  1. ad_封装_ads828

    module ad_ctrl( clk,rst_n,ad_clk, ad_data,value_x,value_y,q_sig,wren,r_addr,w_addr ); input clk; inp ...

  2. [Android]使用RecyclerView替代ListView(四:SeizeRecyclerView)

    以下内容为原创,欢迎转载,转载请注明 来自天天博客:<> [Android]使用RecyclerView替代ListView(四:SeizeRecyclerView) 在RecyclerV ...

  3. Android开发艺术2之Activity的启动模式

    Activity是Android的四大组件之一,他的重要性毋庸置疑,对于这么重要的一个组件,我们首先要知道这些都是由系统进行管理和回调的,要理解Activity的启动模式,我们首先来了解一下Andro ...

  4. .Net 分布式技术比较

    内容转自于 http://www.mamicode.com/info-detail-585547.html .NET 分布式技术比较 1. MSMQ(Microsoft Message Queue) ...

  5. 轻量级代码生成器-OnlyCoder

    程序猿利器:代码生成器,使用代码生成器已经好几年了,增删改查各种生成,从UI到DATA层均生成过.之前有使用过动软的,T4模板等....  T4生成实体还是没有问题的,但是生成MVC视图就有点烦杂了, ...

  6. 浅析Java中synchronized与static synchronized

    synchronized关键字 synchronized是进行同步处理而保证线程安全.在一个方法中,如果是方法内的私有变量,那个这个变量是线程安全的,但是类中的实例变量是可能会出现线程安全问题的,当多 ...

  7. Ubuntu16.04部署python2和python3共存的Jupyter Notebook

    一.安装python和python-pip sudo apt-get install python python3 python-pip python3-pip sudo pip install -- ...

  8. swust oj(0088)表达式的转换

    表达式的转换(0088) Time limit(ms): 5000 Memory limit(kb): 65535 Submission: 435 Accepted: 93 Accepted 16级卓 ...

  9. 一道CVTE前端二面笔试题

    题目:给你一个数组,输出数组中出现次数第n多的数字; 比如:[1,1,1,2,2,2,3,3,4,4,5,5,6,6,7]; 1---3次 2---3次 3---2次 4---2次 5---2次 6- ...

  10. [转]DevExpress GridControl 关于使用CardView的一点小结

    最近项目里需要显示商品的一系列图片,打算用CardView来显示,由于第一次使用,遇到许多问题,发现网上这方面的资源很少,所以把自己的一点点实际经验小结一下,供自己和大家以后参考. 1.选择CardV ...