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. python中string和bool的转换

    python中字符串"True" 和 "False"转为bool类型时, 不能通过bool(xx)强转. 注意是因为在python中,除了&apos;& ...

  2. python文件操作 seek(),tell()

    seek():移动文件读取指针到指定位置 tell():返回文件读取指针的位置 seek()的三种模式: (1)f.seek(p,0)  移动当文件第p个字节处,绝对位置 (2)f.seek(p,1) ...

  3. C++ ORM ODB入门

    1.ORM ORM, Object Relational Mapping, 对象关系映射,用来将基于对象的数据结构映射到SQL的数据结构中.即将基于对象的数据映射到关系表中的字段,然后我们可以通过对象 ...

  4. 配置IIS服务:无法找到该页 您正在搜索的页面可能已经删除、更名或暂时不可用。

    1.配置IIS服务器时,在默认网站创建虚拟目录XXX.然后右击启动页面.aspx,“浏览” 2.  出现错误: 无法找到该页 您正在搜索的页面可能已经删除.更名或暂时不可用. ------------ ...

  5. Ok6410裸机驱动学习(一)开发工具

    1.GCC工具链 1.GCC默认处理的文件类型 文件类型 扩展名 文件说明 文本文件 *.c C语言源文件 *.C.*.cxx.*.cc C++源文件 *.i 预处理后的C语言源文件 *.ii 预处理 ...

  6. USACO-Greedy Gift Givers(贪婪的送礼者)-Section1.2<2>

    [英文原题] Greedy Gift Givers A group of NP (2 ≤ NP ≤ 10) uniquely named friends has decided to exchange ...

  7. 任务调度TimerTask&Quartz的 Java 实现方法与比较

    文章引自--https://www.ibm.com/developerworks/cn/java/j-lo-taskschedule/ 前言 任务调度是指基于给定时间点,给定时间间隔或者给定执行次数自 ...

  8. 基于MapReduce的矩阵乘法

    参考:http://blog.csdn.net/xyilu/article/details/9066973文章 文字未得及得总结,明天再写文字,先贴代码 package matrix; import ...

  9. if if 和 if elif 的区别

    再一次编程中意外使用了if if 也实现了 if elif的功能,所以搜索了下其中的区别: 1.if if 和 if elif 是有区别的,只是在某些情况下才会一样的效果: 2.随意使用会导致意外的错 ...

  10. Spring入门第二十二课

    重用切面表达式 我们有的时候在切面里面有多个函数,大部分函数的切入点都是一样的,所以我们可以声明切入点表达式,来重用. package logan.study.aop.impl; public int ...