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.

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

相关文章推荐

4_Median_of_Two_Sorted_Arrays

LeetCode 4, 2016-11-12

Median of Two Sorted Arrays C++

这道题目很多人认为是leetcode上最经典的题目之一,很早之前就见过这个题目,好像算法导论也有讲过这个题。题意是找两个排好序的数组的中位数,如果不去考虑时间空间复杂度,这个问题极其简单,用merge...

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 t...
  • fly_yr
  • fly_yr
  • 2015年08月22日 20:35
  • 1048

Median of Two Sorted Arrays问题及解法

问题描述: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of ...

Java实现Median of Two Sorted Arrays

求两个有序数据的中位数;Java实现Median of Two Sorted Arrays

每日算法之二: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...
  • yapian8
  • yapian8
  • 2014年03月29日 16:43
  • 773

[leetcode]median of two sorted arrays【寻找第k小的数问题】

Median of Two Sorted ArraysThere are two sorted arrays nums1 and nums2 of size m and n respectively....

Median of Two Sorted Arrays(OO)

There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted ...

Leetcode刷题记—— 4. Median of Two Sorted Arrays(两有序数组的中位数)

一、题目叙述: There are two sorted arrays nums1 and nums2 of size m...

LeetCode (4)Median of Two Sorted Arrays

(4)Median of Two Sorted Arrays题目:两个已经排好序的数组nums1和nums2,大小分别为m和n,求两个已排数组中位数。总运行时间复杂度应该是O(log(m+n))。用例...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Median of Two Sorted Arrays
举报原因:
原因补充:

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