LeetCode4. Median of Two Sorted Arrays---vector实现O(log(m+n)--- findkth
这道题目和PAT上的1029是同一题。但是PAT1029用O(m+n)的时间复杂度(题解)就可以,这道题要求是O(log(m+n))。
这道题花费了我一个工作日的时间来思考。因为是log因而一直思考如何进行二分,想着是两个vector分别二分,但一直想不到合适的解法,就是无穷尽的if else
后来看了题解。思路是从两个有序数组中寻找第k小的数。
<1>花了很长时间在调试上,结果发现是少了一个return,期间还不停怀疑自己写递归的能力。。。要被自己打败了,调代码真是一件细小却很耗费心神的事情。
<2>对vector的使用也是醉了。
思路如下:
我们先假设nums1和nums2数组中元素个数都是大于 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))
考虑实际情况:
如果nums1和nums2数组中元素个数不足 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的更多相关文章
- 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 ...
- 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 ...
- leetcode4 Median of Two Sorted Arrays学习记录
学习了扁扁熊的题解:https://leetcode-cn.com/problems/median-of-two-sorted-arrays/solution/4-xun-zhao-liang-ge- ...
- Leetcode4.Median of Two Sorted Arrays两个排序数组的中位数
给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 . 请找出这两个有序数组的中位数.要求算法的时间复杂度为 O(log (m+n)) . 你可以假设 nums1 和 nums2 不同 ...
- [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 ...
- 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 ...
- 【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 ...
- 《LeetBook》leetcode题解(4): Median of Two Sorted Arrays[H]——两个有序数组中值问题
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- Leetcode 4. Median of Two Sorted Arrays(二分)
4. Median of Two Sorted Arrays 题目链接:https://leetcode.com/problems/median-of-two-sorted-arrays/ Descr ...
- 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 ...
随机推荐
- SEQ序号与ACK序号理解总结
备查 SEQ序号与ACK序号理解总结
- java数组、java.lang.String、java.util.Arrays、java.lang.Object的toString()方法和equals()方法详解
public class Test { public static void main(String[] args) { int[] a = {1, 2, 4, 6}; int[] b = a; in ...
- 学习smart gwt 的一些好的网站
最近在学smart gwt,这个框架和我们比较熟悉的SSH实现思路上有点不一样,因为技术是外国的,所以好多东西都是英文的,正因为是英文的,我们学到的东西才是最多最好的,好了,网站如下: gwt api ...
- 给自己的 MAC 添加一个桌面日历
使用 Ubuntu 做自己的办公环境用了将近三年,最近换了新款的 MBP,系统都用的很舒服. 不过 Ubuntu 是在我的 TP W540上部署的,而 W540 + 电源适配太重了(我的电池是9芯的) ...
- Linux文件管理下
文件操作 对于文件,我们可以读取(read),写入(write)和运行(execute).读取是从已经存在的文件中获得数据.写入是向新的文件或者旧的文件写入数据.如果文件储存的是可执行的二进制码,那么 ...
- filter滤镜的使用
刚开始学css,开始遇到filter不懂什么意思后来到网上查了,觉得解释的很全面,就把它抠下来,以便自己经常来看看. CSS滤镜的使用方法:filter:filtername(parameters) ...
- 【mysql】关于InnoDB表text blob大字段的优化
最近在数据库优化的时候,看到一些表在设计上使用了text或者blob的字段,单表的存储空间已经达到了近100G,这种情况再去改变和优化就非常难了 一.简介 为了清楚大字段对性能的影响,我们必须要知道i ...
- sql中如何分割字符串
使用方式: SELECT AllItem AS BldGUID FROM dbo.fn_split('01.02.03','.') 函数: GO )) )) --实现split功能 的函数 as ...
- ABCD多选正则表达式
正则表达式: 4个选项,可单选可多选不允许重复 ABCD正则: regexp : /^(?!.*((A.*){2,}|(B.*){2,}|(C.*){2,}|(D.*){2,})$)[A-D]{1,4 ...
- 卷积神经网络CNN与深度学习常用框架的介绍与使用
一.神经网络为什么比传统的分类器好 1.传统的分类器有 LR(逻辑斯特回归) 或者 linear SVM ,多用来做线性分割,假如所有的样本可以看做一个个点,如下图,有蓝色的点和绿色的点,传统的分类器 ...