[LeetCode] 4. Median of Two Sorted Arrays(想法题/求第k小的数)
Description
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)).
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
思路
题意:给定两个有序数组,在log级别的复杂度下,求得这两个数组中所有元素的中间值
题解:转换为求第k大的数。
假设A和B的元素个数都大于k/2,我们将A的第k/2个元素(即A[k/2-1])和B的第k/2个元素(即B[k/2-1])进行比较,有以下三种情况(为了简化这里先假设k为偶数,所得到的结论对于k是奇数也是成立的):
- A[k/2-1] == B[k/2-1]
- A[k/2-1] > B[k/2-1]
- A[k/2-1] < B[k/2-1]
如果A[k/2-1] == B[k/2-1],意味着A[0]到A[k/2-1]的肯定在A∪B的top k元素的范围内,换句话说,A[k/2-1]不可能大于A∪B的第k大元素。
因此,我们可以放心的删除A数组的这k/2个元素。
同理,当A[k/2-1] > B[k/2-1]时,可以删除B数组的k/2个元素。
当A[k/2-1] == B[k/2-1]时,说明找到了第k大的元素,直接返回A[k/2-1]或B[k/2-1]即可。
因此,我们可以写一个递归函数。那么函数什么时候应该终止呢?
- 当A或B是空时,直接返回B[k/2-1]或A[k/2-1];
- 当k = 1时,返回min(A[0],B[0]);
- 当A[k/2-1] ==B[k/2-1]时,返回A[k/2-1]或B[k/2-1]
class Solution {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
int len1 = nums1.size(),len2 = nums2.size();
int len = len1 + len2;
if (len & ){
return findKth(nums1,nums2,len / + );
} else{
return (findKth(nums1,nums2,len / ) + findKth(nums1,nums2,len / + ))/;
}
}
double findKth(vector<int> nums1,vector<int> nums2,int k){
int len1 = nums1.size(),len2 = nums2.size();
if (len1 > len2) return findKth(nums2,nums1,k);
if (len1 == ) return nums2[k - ];
if (k == ) return min(nums1[],nums2[]);
int a = min(k / ,len1),b = k - a;
if (nums1[a - ] < nums2[b - ])
return findKth(vector<int>(nums1.begin() + a,nums1.end()),nums2,k - a);
else if (nums1[a - ] > nums2[b - ])
return findKth(nums1,vector<int>(nums2.begin() + b,nums2.end()),k - b);
else return nums1[a - ];
}
};
Java:
public class Solution {
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
int len = nums1.length + nums2.length;
if ((len & ) == ){
return findKth(nums1,nums2,len / + );
}
else{
return (findKth(nums1,nums2,len / ) + findKth(nums1,nums2,len / + )) / ;
}
}
public double findKth(int[] nums1, int[] nums2,int k){
int len1 = nums1.length,len2 = nums2.length;
if (len1 > len2) return findKth(nums2,nums1,k);
if (len1 == ) return nums2[k - ];
if (k == ) return Math.min(nums1[],nums2[]);
int a = Math.min(k / ,len1),b = k - a;
if (nums1[a - ] < nums2[b - ]) return findKth(Arrays.copyOfRange(nums1, a, len1),nums2,k - a);
else if (nums1[a - ] > nums2[b - ]) return findKth(nums1,Arrays.copyOfRange(nums2,b,len2), k - b);
else return nums1[a - ];
}
}
[LeetCode] 4. Median of Two Sorted Arrays(想法题/求第k小的数)的更多相关文章
- 【算法之美】求解两个有序数组的中位数 — 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】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 s ...
- 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之 median of two sorted arrays
这是我做的第二个leetcode题目,一开始以为和第一个一样很简单,但是做的过程中才发现这个题目非常难,给人一种“刚上战场就踩上地雷挂掉了”的感觉.后来搜了一下leetcode的难度分布表(leetc ...
- [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(二分)
4. Median of Two Sorted Arrays 题目链接:https://leetcode.com/problems/median-of-two-sorted-arrays/ Descr ...
- LeetCode题解-----Median of Two Sorted Arrays
题目描述: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of t ...
- Leetcode 解题 Median of Two sorted arrays
题目:there are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...
随机推荐
- Bootstrap真的总是好的吗
原文地址:Bootstrap considered harmful 原文作者:Hidde de Vries 译文出自:neal 译者: Neal 个人主页:http://neal1991.python ...
- MySQL Server类型的MySQL 客户端的下载、安装和使用
mysql server 5.5的下载 下载地址:https://dev.mysql.com/downloads/mysql/5.0.html#linux mysql server 5.5的安装 双击 ...
- VirtualBox中安装CentOS 7
1.如下所示图,点击“新建”,创建一个新的虚拟机 2.类型选择Linux,版本选择Red Hat,下一步 3.分配内存大小,电脑8G内存,所以分给虚拟机2G,选择下一步 4.选择“现在创建虚拟硬盘” ...
- Simple Live System Using Nginx
1. Install nginx #Preinstalled directory install=/usr/local/nginx #Delete installed directory rm -rf ...
- Lamabda Where Select Find First等区别
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...
- Sass @debug
@debug 在 Sass 中是用来调试的,当你的在 Sass 的源码中使用了 @debug 指令之后,Sass 代码在编译出错时,在命令终端会输出你设置的提示 Bug: @debug 10em + ...
- uboot中Kconfig架构的理解
1./u-boot-2019.07/Kconfig 是顶层Kconfig mainmenu "U-Boot $UBOOTVERSION Configuration" #这是总me ...
- Springboot 默认cache
1:Springboot 默认缓存为ConcurrentMapCacheManager(spring-context) 2:再启动类上开启缓存 @SpringBootApplication //相当于 ...
- $2019$ 暑期刷题记录1:(算法竞赛DP练习)
$ 2019 $ 暑期刷题记录: $ POJ~1952~~BUY~LOW, BUY~LOWER: $ (复杂度优化) 题目大意:统计可重序列中最长上升子序列的方案数. 题目很直接的说明了所求为 $ L ...
- 给mongodb设置密码
内容来自:https://segmentfault.com/a/1190000011554055 mongodb安装后是无需密码 Mongodb安装后自身是没有密码的,用户连接只需填写id地址,端口号 ...