004 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)).
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
详见:https://leetcode.com/problems/median-of-two-sorted-arrays/description/
Java实现:
方法一:常规解法
import java.util.Arrays;
class Solution {
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
int size1=nums1.length;
int size2=nums2.length;
int size=size1+size2;
int[] nums = new int[size];
for(int i = 0; i<size1; i++){
nums[i] = nums1[i];
}
for(int i = size1; i<size; i++){
nums[i] = nums2[i-size1];
}
Arrays.sort(nums);
double median;
if((size-1)%2 == 0){
median = nums[(size-1)/2] * 1.0;
} else {
median = (nums[(size-1)/2] + nums[((size-1)/2)+1])/2.0;
}
return median;
}
}
方法二:求第k小的数
public class Solution {
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
int size1 = nums1.length;
int size2 = nums2.length;
int size = size1 + size2;
if(size % 2 == 1){
return findKth(nums1, 0, size1, nums2, 0, size2, size / 2 + 1);
}else{
return (findKth(nums1, 0, size1, nums2, 0, size2, size / 2) + findKth(nums1, 0, size1, nums2, 0, size2, size / 2 + 1)) /2;
}
}
public double findKth(int[] nums1, int start1, int size1, int[] nums2, int start2, int size2, int k){
if(size1 - start1 > size2 -start2){
return findKth(nums2, start2, size2, nums1, start1, size1, k);
}
if(size1 - start1 == 0){
return nums2[k - 1];
}
if(k == 1){
return Math.min(nums1[start1], nums2[start2]); // k==1表示已经找到第k-1小的数,下一个数为两个数组start开始的最小值
}
int p1 = start1 + Math.min(size1 - start1, k / 2); // p1和p2记录当前需要比较的那个位
int p2 = start2 + k - p1 + start1;
if(nums1[p1 - 1] < nums2[p2 - 1]){
return findKth(nums1, p1, size1, nums2, start2, size2, k - p1 + start1);
}else if(nums1[p1 - 1] > nums2[p2 -1]){
return findKth(nums1, start1, size1, nums2, p2, size2, k - p2 + start2);
}else{
return nums1[p1 - 1];
}
}
}
C++实现:
class Solution {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
int size1=nums1.size();
int size2=nums2.size();
int size=size1+size2;
if(size&0x1)
return findKTh(nums1.begin(),size1,nums2.begin(),size2,size/+);
else
return (findKTh(nums1.begin(),size1,nums2.begin(),size2,size/)+findKTh(nums1.begin(),size1,nums2.begin(),size2,size/+))/;
}
double findKTh(vector<int>::iterator nums1,int m,vector<int>::iterator nums2,int n,int k)
{
if(m>n)
return findKTh(nums2,n,nums1,m,k);
if(m==)
return *(nums2+k-);
if(k==)
return min(*nums1,*nums2);
int pa=min(k/,m),pb=k-pa;
if(*(nums1+pa-)<*(nums2+pb-))
return findKTh(nums1+pa,m-pa,nums2,n,k-pa);
else if(*(nums1+pa-)>*(nums2+pb-))
return findKTh(nums1,m,nums2+pb,n-pb,k-pb);
else
return *(nums1+pa-);
}
};
参考:
https://www.cnblogs.com/leavescy/p/5877627.html
https://www.cnblogs.com/bakari/p/5082155.html
http://blog.csdn.net/yutianzuijin/article/details/11499917/
004 Median of Two Sorted Arrays 两个有序数组的中位数的更多相关文章
- [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 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 two ...
- [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 ...
- Leetcode4.Median of Two Sorted Arrays两个排序数组的中位数
给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 . 请找出这两个有序数组的中位数.要求算法的时间复杂度为 O(log (m+n)) . 你可以假设 nums1 和 nums2 不同 ...
- 2.Median of Two Sorted Arrays (两个排序数组的中位数)
要求:Median of Two Sorted Arrays (求两个排序数组的中位数) 分析:1. 两个数组含有的数字总数为偶数或奇数两种情况.2. 有数组可能为空. 解决方法: 1.排序法 时间复 ...
- 【medium】4. Median of Two Sorted Arrays 两个有序数组中第k小的数
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 ...
- 4. Median of Two Sorted Arrays(2个有序数组的中位数)
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...
- Median of Two Sorted 求两个有序数组的中位数
中位数是把一个数的集合划分为两部分,每部分包含的数字个数相同,并且一个集合中的元素均大于另一个集合中的元素. 因此,我们考虑在一个任意的位置,将数组A划分成两部分.i表示划分数组A的位置,如果数组A包 ...
随机推荐
- 洛谷【P1873】砍树
我对二分的理解:https://www.cnblogs.com/AKMer/p/9737477.html 题目传送门:https://www.luogu.org/problemnew/show/P18 ...
- bzoj 2648 SJY摆棋子 —— K-D树
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2648 学习资料:https://blog.csdn.net/zhl30041839/arti ...
- TFS自定义开发中的反射应用
最近CM(Configuration Management) 的同事在自定义开发TFS的过程中遇到一个问题. 领导要求快速开发一个工具, 可以自动连接TFS,然后自动Check out一些word文件 ...
- ResultSetMetaData和ResultSet
我现在有一张表t_product;我们查询所有的商品:SELECT * FROM t_product; 上述所有的数据都可以封装成一个对象,我们称这个查询出来的对象为结果集对象:ResultSet. ...
- Hadoop中Partition的定制
1.解析Partition Map的结果,会通过partition分发到Reducer上,Reducer做完Reduce操作后,通过OutputFormat,进行输出,下面我们就来分析参与这个过程的类 ...
- 1.4 DVWA亲测XSS漏洞
首先需要有配置好的DVWA环境,像下图这样 其中: XSS (DOM) : DOM型XSS漏洞 XSS (Reflected) : 反射性XSS漏洞 XSS (Stored) : 存储型XS ...
- c#事件1
Private void button_clicked( object sender ,RouteEventArgs e) sender :引发事件的对象 源 e : 路由事件,提供可能重要 ...
- Dijkstra 路径规划 C#
示例无向图如下:(起始点为v0) 邻接矩阵为: 注意:其中没有连接的边和自己到自己的点权值用10000表示. 代码: static void Main(string[] args) { , , , , ...
- 关于nginx的使用感想
感慨: 最近老板说要转型NodeJs,所以接连用ThinkJS框架写了2个项目(不过通篇弱类型不用考虑class是真的爽啊...).那天老板又正好让我看看Ngxin的负载均衡和静态资源处理,那现在就写 ...
- 机器学习十大算法之EM算法
此文已由作者赵斌授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 由于目前论坛的Markdown不支持Mathjax,数学公式没法正常识别,文章只能用截图上传了... ...