Median of Two Sorted Arrays

原创 2015年07月09日 22:39:59

Median of Two Sorted Arrays
很经典的题目
题目要求时间复杂度O(log(m+n))
看别人的思路写出来的,核心是findkth方法的递归
不止是中位数,所有的找第几个大的数都可以用这个方法

代码块

public class Solution {
    public double findMedianSortedArrays(int[] nums1, int[] nums2) {
        int aend=nums1.length-1;
        int bend=nums2.length-1;
        int astart=0,bstart=0;
        int total=nums1.length+nums2.length;
        if(total%2==1){
            return findkth(nums1,nums2,total/2+1,astart,aend,bstart,bend);
        }else{
            return (findkth(nums1,nums2,total/2+1,astart,aend,bstart,bend)+
            findkth(nums1,nums2,total/2,astart,aend,bstart,bend))/2;
        }
        }
        public double findkth(int a[],int b[],int k,int astart,int aend,int bstart,int bend){
            if(aend-astart>bend-bstart){
                return findkth(b,a,k,bstart,bend,astart,aend);//保持数组a是长度较小的那一个
            }
            if(aend-astart+1==0){//若a数组为空,那么就直接返回b的第k个数
                return b[bstart+k-1];
            }

            if(k==1&&aend-astart+1!=0){//若k=1 那么就直接返回a,b第一个较小的数
                return Math.min(a[astart],b[bstart]);
            }
            int pa=Math.min(k/2,aend-astart+1),pb=k-pa;//将k分为两部分,如果a的第k/2个数比b的第K/2个数小,那么直接把a的前k/2个数去掉
            if(a[astart+pa-1]<b[bstart+pb-1]){        //同时k也减去k/2,继续递归,直到a的长度为空或者k为1,结束递归
                astart+=pa;
                return findkth(a,b,k-pa,astart,aend,bstart,bend);
            }else if(a[astart+pa-1]>b[bstart+pb-1]){
                bstart+=pb;
                return findkth(a,b,k-pb,astart,aend,bstart,bend);
            }else{
                return a[astart+pa-1];
            }

            }
}

核心思想
Assume that the number of elements in A and B are both larger than k/2, and if we compare the k/2-th smallest element in A(i.e. A[k/2-1]) and the k-th smallest element in B(i.e. B[k/2 - 1]), there are three results:
(Becasue k can be odd or even number, so we assume k is even number here for simplicy. The following is also true when k is an odd number.)
A[k/2-1] = B[k/2-1]
A[k/2-1] > B[k/2-1]
A[k/2-1] < B[k/2-1]
if A[k/2-1] < B[k/2-1], that means all the elements from A[0] to A[k/2-1](i.e. the k/2 smallest elements in A) are in the range of k smallest elements in the union of A and B. Or, in the other word, A[k/2 - 1] can never be larger than the k-th smalleset element in the union of A and B.

We should also consider the edge case, that is, when should we stop?
1. When A or B is empty, we return B[k-1]( or A[k-1]), respectively;
2. When k is 1(when A and B are both not empty), we return the smaller one of A[0] and B[0]
3. When A[k/2-1] = B[k/2-1], we should return one of them

In the code, we check if m is larger than n to garentee that the we always know the smaller array, for coding simplicy.

版权声明:本文为博主原创文章,未经博主允许不得转载。

两个有序数组的中位数Median of Two Sorted Arrays(很重要)

https://leetcode.com/problems/median-of-two-sorted-arrays/ 对于一个长度为n的已排序数列a,若n为奇数,中位数为a[n / 2 + 1]...
  • gao1440156051
  • gao1440156051
  • 2016年06月21日 11:11
  • 3208

Leetcode全数组问题

目录 1、编号2 Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respe...
  • wangxiaojun911
  • wangxiaojun911
  • 2014年02月06日 03:32
  • 6318

LintCode数组题总结

做算法题的时候,几乎不可避免要跟数组打交道。在LintCode上数组那一章有这么一些题目: 1)547. Intersection of Two Arrays 比较简单。要求找到2个数组的...
  • luoshengkim
  • luoshengkim
  • 2016年06月02日 23:16
  • 1794

LeetCode4 Median of Two Sorted Arrays

  • 2018年01月09日 20:07
  • 7KB
  • 下载

Median of Two Sorted Arrays(两个顺序数组求中位数)

Example 1: nums1 = [1, 3] nums2 = [2] The median is 2.0 Example 2: nums1 = [1, 2] nums2 =...
  • kh971024
  • kh971024
  • 2018年01月10日 20:44
  • 13

Find the median of two sorted arrays(找到两个排好序的数组的中位数)

原题是这样的: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the media...
  • wuh2333
  • wuh2333
  • 2016年09月19日 14:28
  • 351

Median of Two Sorted Arrays(两个有序数组的中位数)

There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...
  • KaJIU
  • KaJIU
  • 2017年03月06日 21:28
  • 72

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 ...
  • ylifam
  • ylifam
  • 2017年03月07日 00:08
  • 131

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 so...
  • skyoceanlover
  • skyoceanlover
  • 2014年06月20日 21:10
  • 476

【leetcode】Median of Two Sorted Arrays 如果觉得网上其他解释太难,那么请看这篇

Median of Two Sorted Arrays 这个题太难了,看到方法之后都不会编的那种,及其复杂,各种边界条件。 首先,要把这个问题转化为求两个有序数组中第K个数的问题; 求第K个...
  • deepbluech
  • deepbluech
  • 2014年09月06日 17:30
  • 453
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Median of Two Sorted Arrays
举报原因:
原因补充:

(最多只允许输入30个字)