这道题目和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. Nginx的配置文件详解

    主配置文件: 查看nginx的进程可以看到nginx所使用的配置文件: 主配置一般会被用来设置一些全局的参数: 参数详解: user nobody nobody;          //设置nginx ...

  2. 必须要推荐的浏览器插件---作者:marsggbo

          首先需要说清楚,绝对没有打广告.反反复复用了好多浏览器,换来换去,最后还是留下了chrome浏览器和百度浏览器以及Egde浏览器(不想留也没办法).下面就说说实用的插件吧.      百度 ...

  3. 关于开发环境中的消息在download时没有下载下来时的问题

    业务场景:在开发环境改了一些代码,现在需要将这些代码(包括class和数据库对象)移植到开发环境,整理出了Objectlist(就是该模块定义了哪些数据库对象),然后上传到FTP服务器上时,再执行do ...

  4. js两个判断&&的值与||的值

    var value1="val1"; var value2="val2"; alert(value1&&value2);    //结果为val ...

  5. jQuery控制input不可编辑

    1.开启disabled,是input不可以编辑 $("#id").attr("disabled","disabled"); 2.关闭dis ...

  6. 11.Linux用户管理

    本笔记说的Linux用户管理主要包括以下内容: 1.用户配置文件(/etc/passwd  /etc/shadow) 2.组配置文件(/etc/group  /etc/gshadow) 3.用户缺省配 ...

  7. 基于Excel参数化你的Selenium2测试代码

  8. coolpad 5879logcat不能输入日志解决办法

    有几天没完手机了,玩一下,发现不能打印日志了,记得最开始的时候 会弹出一个选项选择是否输出日志,在网上找了好的方法. 1.重启adb,点击DBMS 进行刷新. 2重启eclipse. 3.重装驱动. ...

  9. C#遍历指定文件夹中的所有文件(转)

    原文链接:http://www.cnblogs.com/qianqianfy/archive/2009/07/08/1518974.html 1. C#遍历指定文件夹中的所有文件 DirectoryI ...

  10. (iOS)谈谈关于使用category的静态库(原创)

    最近在一个项目中使用了一个包含catecategory 的静态库,但是此项目在运行过程中,该静态库调用 category 增加的方法处,却报 selector not recognized 异常,会直 ...