这道题目和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. 常见【十种】WEB攻击及防御技术总结

    最近参加了一个WEB安全培训,感觉WEB攻击跟防御都是挺有意思的.以下总结比较简短,仅供观赏哈. 一.XSS攻击 [介绍] xss攻击是跨站脚本攻击,例如在表单中提交含有可执行的javascript的 ...

  2. 为什么要学Python

    人生苦短,我用python.在大学四年的本科学习中,Python是我接触过语法最简单,功能最为强大的语言,拥有众多第三方库的支持的语言.如果要选一门编程语言作为入门,建议使用Python.但是为了更加 ...

  3. centos 6.5 搭建ftp服务器

    linux下一般使用vsftpd作为ftp服务器. vsftpd是一款在Linux发行版中最受推崇的FTP服务器程序.特点是小巧轻快,安全易用. 下面是安装配置步骤: 1.安装vsftpd yum i ...

  4. 【转】FLEX中SharedObject介绍及应用

    ShareObject介绍: 1 ShareObject,顾名思义共享对象,而通常意义上的共享,从B/S结构上来讲,无非是客户端(浏览器端)的共享和服务器端的共享了,不错,ShareObject刚好份 ...

  5. Zookeeper的安装和初步使用

    1. Zookeeper集群角色 Zookeeper集群的角色:  Leader 和  follower  (Observer) zk集群最好配成奇数个节点 只要集群中有半数以上节点存活,集群就能提供 ...

  6. dotnet Core 调用HTTP接口,系统大量CLOSE_WAIT连接问题的分析,尚未解决。

    环境: dotnet core 1.0.1 CentOS 7.2 今天在服务器巡检的时候,发现一个服务大量抛出异常 异常信息为: LockStatusPushError&&Messag ...

  7. ajax 大洋与小样的第二步

    一.Ajax的对象 XMLHttpRequest的方法 方法 描述 abort() 停止当前请求 getAllResponseHeaders() 把 HTTP请求的所有响应首部作为健/值对返回 get ...

  8. qt5的.ui文件在VS2010中无法编译问题

    自己手动添加的.ui文件在VS中是无法右键编译的,也即是说,在用QT designer编辑过的.ui文件无法实时更新相应的ui_XX.h文件,造成调试结果无法显示编辑过的新界面. 解决办法: 右键.u ...

  9. 24(java_io from keyboard)

    public class ReadFromKB{ public static void main(String args[]) { try { byte bArray[]=new byte[128]; ...

  10. 为什么不能将客户端的连接请求跳转或转发到本机lo回环接口上?

    一.为什么不能将本机的请求跳转/转发到回环接口上? 如上图一样,服务器对外只开放了一个80端口,但是web服务监听在了lo 接口上8080端口上,现在要实现外网通过访问服务器的80端口,来提供web服 ...