Leetcode 4. Median of Two Sorted Arrays(中位数+二分答案+递归)
There are two sorted arrays nums1 and nums2 of size m and n respectively.
Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).
You may assume nums1 and nums2 cannot be both empty.
Example 1:
nums1 = [1, 3]
nums2 = [2] The median is 2.0
Example 2:
nums1 = [1, 2]
nums2 = [3, 4] The median is (2 + 3)/2 = 2.5 思路:
中位数,其实就是找到第k个大小的元素的特例。在单数组中实现方式简单,关键是如何在两个数组中找到第k大的元素。难就难在要在两个未合并的有序数组之间使用二分法,这里我们需要定义一个函数来找到第K个元素,由于两个数组长度之和的奇偶不确定,因此需要分情况来讨论,对于奇数的情况,直接找到最中间的数即可,偶数的话需要求最中间两个数的平均值。下面重点来看如何实现找到第K个元素,首先我们需要让数组1的长度小于或等于数组2的长度,那么我们只需判断如果数组1的长度大于数组2的长度的话,交换两个数组即可,然后我们要判断小的数组是否为空,为空的话,直接在另一个数组找第K个即可。还有一种情况是当K = 1时,表示我们要找第一个元素,只要比较两个数组的第一个元素,返回较小的那个即可。
首先假设数组A和B的元素个数都大于k/2,我们比较A[k/2-1]和B[k/2-1]两个元素,这两个元素分别表示A的第k/2小的元素和B的第k/2小的元素。这两个元素比较共有三种情况:>、<和=。如果A[k/2-1]大于B[k/2-1],则A[k/2-1]小于合并之后的第k小值。
证明也很简单,可以采用反证法。假设A[k/2-1]大于合并之后的第k小值,我们不妨假定其为第(k+1)小值。由于A[k/2-1]小于B[k/2-1],所以B[k/2-1]至少是第(k+2)小值。但实际上,在A中至多存在k/2-1个元素小于A[k/2-1],B中也至多存在k/2-1个元素小于A[k/2-1],所以小于A[k/2-1]的元素个数至多有k/2+ k/2-2,小于k,这与A[k/2-1]是第(k+1)的数矛盾。
同理当A[k / 2 - 1] > B[k / 2 -1]时存在类似的结论
当A[k / 2 - 1] = B[k / 2 -1]时,表示,在在A的k/2 -1之前已经有k/2 -1和数小于A[k / 2 -1],同理在B 之前也是一样的,所以此时已经找到了第k小的数,即这个相等的元素。
class Solution {
public:
double findKth(vector<int>& nums1, int p1, vector<int>& nums2, int p2, int k){
int len1 = nums1.size(), len2 = nums2.size();
if(len1-p1>len2-p2){
return findKth(nums2,p2,nums1,p1,k);
}
if(len1==p1){
return nums2[p2+k-];
}
if(k==){
return min(nums1[p1],nums2[p2]);
}
int l1 = min(k/ + p1,len1), l2 = p2 + k - l1 + p1;;
int max1 = nums1[l1-], max2 = nums2[l2-];
if(max1==max2) return max1;
else if(max1<max2){
return findKth(nums1,l1,nums2,p2,k-l1+p1);
}
else {
return findKth(nums1,p1,nums2,l2,k-l2+p2);
}
}
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
int len1 = nums1.size(),len2 = nums2.size();
if((len1+len2)%==){
return (findKth(nums1,,nums2,,(len1+len2)/)+findKth(nums1,,nums2,,(len1+len2+)/))/;
}
else{
return findKth(nums1,,nums2,,(len1+len2+)/);
}
}
};
#下面这个代码思路完全一样,加了一些注释,是来自别人的博客,附上原地址,感谢大佬:https://www.cnblogs.com/mini-coconut/p/9066508.html 1 double findKth(vector<int> &nums1, int i, vector<int> &nums2, int j, int k)
{
// 首先需要让数组1的长度小于或等于数组2的长度
if (nums1.size() - i > nums2.size() - j) {
return findKth(nums2, j, nums1, i, k);
}
// 判断小的数组是否为空,为空的话,直接在另一个数组找第K个即可
if (nums1.size() == i) {
return nums2[j + k - ];
}
// 当K = 1时,表示我们要找第一个元素,只要比较两个数组的第一个元素,返回较小的那个即可
if (k == ) {
return min(nums1[i], nums2[j]);
}
int pa = min(i + k / , int(nums1.size())), pb = j + k - pa + i; if (nums1[pa - ] < nums2[pb - ]) {
return findKth(nums1, pa, nums2, j, k - pa + i);
}
else if (nums1[pa - ] > nums2[pb - ]) {
return findKth(nums1, i, nums2, pb, k - pb + j);
}
else {
return nums1[pa - ];
}
}
double findMedianSortedArrays(vector<int> A, vector<int> B) {
int sizeA = A.size(), sizeB = B.size();
if (sizeA <= && sizeB <= ) {
return ;
}
int total = sizeA + sizeB;
if (total % == ) {
return findKth(A, , B, , total / + );
}
else {
return (findKth(A, , B, , total / ) + findKth(A, , B, , total / + )) / ;
}
}
这里比较难理解的点是判断(nums1[pa - 1] < nums2[pb - 1])之后执行了return findKth(nums1, pa, nums2, j, k - pa + i);其实这个操作是因为目前nums1的分界线的值小于nums2分界线的值,那么证明nums1分界线以及前面的值都小于合并后的第k的值,也就是中位数。那么我们可以从这里开始,继续寻找第k-(pa-i)的值,直到两个值相等为止。
Leetcode 4. Median of Two Sorted Arrays(中位数+二分答案+递归)的更多相关文章
- Leetcode 4. Median of Two Sorted Arrays(二分)
4. Median of Two Sorted Arrays 题目链接:https://leetcode.com/problems/median-of-two-sorted-arrays/ Descr ...
- 【算法之美】求解两个有序数组的中位数 — leetcode 4. Median of Two Sorted Arrays
一道非常经典的题目,Median of Two Sorted Arrays.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/ ...
- LeetCode(3) || Median of Two Sorted Arrays
LeetCode(3) || Median of Two Sorted Arrays 题记 之前做了3题,感觉难度一般,没想到突然来了这道比较难的,星期六花了一天的时间才做完,可见以前基础太差了. 题 ...
- LeetCode 4 Median of Two Sorted Arrays (两个数组的mid值)
题目来源:https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 an ...
- LeetCode 4. Median of Two Sorted Arrays & 归并排序
Median of Two Sorted Arrays 搜索时间复杂度的时候,看到归并排序比较适合这个题目.中位数直接取即可,所以重点是排序. 再来看看治阶段,我们需要将两个已经有序的子序列合并成一个 ...
- 第三周 Leetcode 4. Median of Two Sorted Arrays (HARD)
4. Median of Two Sorted Arrays 给定两个有序的整数序列.求中位数,要求复杂度为对数级别. 通常的思路,我们二分搜索中位数,对某个序列里的某个数 我们可以在对数时间内通过二 ...
- LeetCode 004 Median of Two Sorted Arrays
题目描述:Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. F ...
- [LeetCode] 4. Median of Two Sorted Arrays 两个有序数组的中位数
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...
- leetcode 4 : Median of Two Sorted Arrays 找出两个数组的中位数
题目: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...
随机推荐
- input标签内容改变时触发事件
1. onchange事件与onpropertychange事件的区别: onchange事件在内容改变(两次内容有可能相等)且失去焦点时触发: onpropertychange事件是实时触发,每增加 ...
- Canvas入门03-绘制弧线和圆
绘制弧线的API: context.arc(centerx:number, centery: number, radius: number, startAngle: number, endAngle: ...
- Java IO NIO详细讲解
1.IO Java IO概述 2.NIO Java NIO浅析
- jQuery中this与$(this)的区别总结
这里就谈谈this与$(this)的区别. 1.jQuery中this与$(this)的区别 $("#textbox").hover( function() { this.titl ...
- [转帖]又一国产x86处理器可大规模上市:Intel至强核心 安全监测管控
又一国产x86处理器可大规模上市:Intel至强核心 安全监测管控 https://www.cnbeta.com/articles/tech/850525.htm 不知道是不是有一起汉芯事件 国产CP ...
- [转帖]同事推荐的的aira2
Windows系统安装最新版Aria2客户端及使用教程 https://www.moerats.com/archives/519/ 改天学习一下. 说明:之前都是说的在Linux VPS服务器上安装A ...
- 字符串中的replace方法
String.prototype.replace() 该方法作为字符串中非常常用的方法, 今天我们具体介绍一下它的用法 语法格式 someString.replace(regxp | substr, ...
- 由于;引发的Oracle的BadSqlExecption
一.在使用单引号进行各种转换合并的时候发生的异常: 1:首先单引号的表示符为 ' 在Oracle中如果SQL语句带上了分号是会报错的, 在SQL传入进行执行的时候会报BadSQLExecption,这 ...
- [BZOJ 4025]二分图(线段树分治+带边权并查集)
[BZOJ 4025]二分图(线段树分治+带边权并查集) 题面 给出一个n个点m条边的图,每条边会在时间s到t出现,问每个时间的图是否为一个二分图 \(n,m,\max(t_i) \leq 10^5\ ...
- 针对Vue相同路由不同参数的刷新问题
在使用vue和vue-router开发spa应用时,我们会遇到这样一种问题.当页面跳转时,组件本身并没有发生改变: // 路由映射关系'/form/:type' // 当前页面路由/form/shop ...