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 两个有序数组的中位数的更多相关文章

  1. [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 ...

  2. [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 ...

  3. [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 ...

  4. Leetcode4.Median of Two Sorted Arrays两个排序数组的中位数

    给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 . 请找出这两个有序数组的中位数.要求算法的时间复杂度为 O(log (m+n)) . 你可以假设 nums1 和 nums2 不同 ...

  5. 2.Median of Two Sorted Arrays (两个排序数组的中位数)

    要求:Median of Two Sorted Arrays (求两个排序数组的中位数) 分析:1. 两个数组含有的数字总数为偶数或奇数两种情况.2. 有数组可能为空. 解决方法: 1.排序法 时间复 ...

  6. 【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 ...

  7. 【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 ...

  8. 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 ...

  9. Median of Two Sorted 求两个有序数组的中位数

    中位数是把一个数的集合划分为两部分,每部分包含的数字个数相同,并且一个集合中的元素均大于另一个集合中的元素. 因此,我们考虑在一个任意的位置,将数组A划分成两部分.i表示划分数组A的位置,如果数组A包 ...

随机推荐

  1. html之ajax

    正常情况下,html中的ajax(也就是XMLHttpRequest对象)是不能跨域的.(特殊情况,此处不讨论,请网上Google) ---跨域:是url的协议或ip或端口,其中有一个不同,就是跨域. ...

  2. Impala的JDBC无法连接

    这是因为客户端连接的JDBC是Impala的master机器,而不是DataNode:因为JDBC的服务宿主是Impalad,而Impalad只是部署在DataNode

  3. 【转】 Pro Android学习笔记(七六):服务(1):local和remote

    文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://blog.csdn.net/flowingflying/ Android提供服务,服务是运行在后台的 ...

  4. phpMailer在CentOS 6.5下无法发送邮件的解决办法

    作者:ffsystem 网站在Windows平台上开发测试,完成了后同步到CentOS6.5 Nigix运行.发现phpMailer组件无法与SMTP服务器建立连接,导致无法发送邮件. 错误代码: p ...

  5. Spring 学习十五 AOP

    http://www.hongyanliren.com/2014m12/22797.html 1: 通知(advice): 就是你想要的功能,也就是安全.事物.日子等.先定义好,在想用的地方用一下.包 ...

  6. 基于STM32的三轴数字罗盘HMC5883L模块的测试

    最近买了个数字罗盘模块,调通后发现很不错,非常灵敏,测试的时候精度在1°以内.连续测量模式下,最快测量.输出速率可达75hz,模块每次测量完毕并将数据更新至寄存器后,其DRDY引脚便产生一个低电平脉冲 ...

  7. Java探索之旅(17)——多线程(1)

    1.多线程  1.1线程 线程是程序运行的基本执行单元.指的是一段相对独立的代码,执行指定的计算或操作.多操作系统执行一个程序时会在系统中建立一个进程,而在这个进程中,必须至少建立一个线程(这个线程被 ...

  8. AVI编码器

    AVI编码器,AVI英文全称为Audio Video Interleaved,即音频视频交错格式.就是编码语音和影像同步组合在一起的文件格式.它对视频文件采用了一种有损压缩方式,但压缩比较高,因此尽管 ...

  9. #425[div2]

    A 签到 #include<bits/stdc++.h> using namespace std; typedef long long ll; int main(){ ll n,k; ci ...

  10. 对vuex的理解

    我用的vue安装了一个插件vuex插件 有3个 文件夹分别是actions(用于数据请求),getters(用于监听store),store(用于存储数据),