LeetCode【4】. Median of Two Sorted Arrays --java的不同方法实现

Median of Two Sorted Arrays

这道题确实有点难,想挺久,看别人答案也是不能一下子就明白。题目难度为Hard,原题如下:

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

给定两个已排序的数组,求两个数组整体的中位数。时间复杂度要求为O(log
(m+n))。

一、思路:

将本体求中位数的要求调整为将两个数组整体合并排序后求其第k个数。下面用一个例子来说明:

图一、例子示意图

A班和B班的同学人数分别为aEnd与bEnd,有天体育老师说要找他们中间第k矮的同学,于是先将两班的同学按从小到大的顺序站成两队。假设k是为13。那此时A班派出第x矮的同学,B班派出第y矮的同学来比较(注意:此时x+y<=k)。

若x为6,y为7。如果A的第6位同学比B的第7位同学还要矮,那如果两班整体排序,那A班的6位同学一定是要站在B班的第7位前面,最多A班第6位就站在B班第7位的前一位,最多就整体排第12,怎么都轮不到他排第k=13个。于是可以把A班前6位丢掉,在剩下的两个队列里面找。但是就不再是找第k个,而是找第k-x个了。因为x做了基数被丢掉了。

当然,上述例子,只要(x+y)<=k的情况,x与y只要是为非负的,那都可以。只是为了效率,一般去x=y=(k/2)。而当某一队被丢到剩下人很少或者没人了,那这个取值就要根据具体情况来取值。

二、java 程序:

public class Solution {
    public double findMedianSortedArrays(int[] nums1, int[] nums2) {
        int m = nums1.length;
        int n = nums2.length;
        int k = m + n;

        if((k&1)==1)
        {
            return find(nums1, 0, m, nums2, 0, n, k/2+1);
        }else
            return (find(nums1, 0, m, nums2, 0, n, k/2)+find(nums1, 0, m, nums2, 0, n, k/2+1))/2;
    }
    //递归算法,不断缩小两个数组的范围,同时k的值也相对两个搜索区间上限起始点而改变
    public double find(int[] A, int aStart, int aEnd, int[] B, int bStart, int bEnd,int kth)
    {
    //1. 统一将长度短的放置于find函数参数的前面项
        if(aEnd>bEnd)
            return find(B, bStart, bEnd, A, aStart, aEnd, kth);

    //2. 长度短的为空,则答案等同于求另外一个数组的中位数
        if(aEnd<=0)
            return B[bStart + kth -1];

    //3. 递归到终点,两个数组的aStart和bStart已经到了中位数的位置
        if(kth==1)
            return min(A[aStart],B[bStart]);

        int pa = min(kth/2,aEnd), pb = kth-pa;

        if(A[aStart + pa-1]< B[bStart +pb -1])
            return find(A, aStart+pa, aEnd-pa, B, bStart, bEnd, kth-pa );
        else
            return find(A, aStart, aEnd, B, bStart + pb, bEnd - pb,kth-pb);

    }
    public int min(int a, int b)
    {
        return a>b?b:a;
    }
}

参考:LeetCode
– Median of Two Sorted Arrays Java

Median
of Two Sorted Arrays (JAVA)

时间: 2024-08-07 21:20:30

LeetCode【4】. Median of Two Sorted Arrays --java的不同方法实现的相关文章

LeetCode 004 Median of Two Sorted Arrays - Java

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,

LeetCode(3) || Median of Two Sorted Arrays

LeetCode(3) || Median of Two Sorted Arrays 题记 之前做了3题,感觉难度一般,没想到突然来了这道比较难的,星期六花了一天的时间才做完,可见以前基础太差了. 题目内容 There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should

【题解】【数组】【Leetcode】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 sorted arrays. The overall run time complexity should be O(log (m+n)). ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

LeetCode OJ - 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 arrays. The overall run time complexity should be O(log (m+n)). 解题思路: 将原问题转变成一个寻找第k小数的问题(假设两个原序列升序排列),这样中位数实际上是第(m+n)/2小的数.所以只要解决了第k小数的问题,原问题也得以解决

[LeetCode][JavaScript]Median of Two Sorted Arrays

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)). https://leetcode.com/problems/median-of-two-sorted

第三周 leetcode 4. Median of Two Sorted Arrays (HARD)

4. Median of Two Sorted Arrays 给定两个有序的整数序列.求中位数,要求复杂度为对数级别. 通常的思路,我们二分搜索中位数,对某个序列里的某个数 我们可以在对数时间内通过二分算法求得两个序列中比它小的数,整体复杂度也是对数级别.但是代码实现较为困难. 换一个思路,我们把中位数不要当作一个数,而当作一个序列的划分.划分后,序列的左半部设为L,右半部设为R 满足max(L)<=min(R)且满足len(L)==len(R) 二分搜索这个划分即可.对于A+B的长度为奇数的情

leetcode 2.Median of Two Sorted Arrays

Median of Two Sorted Arrays Problem: There are two sorted arrays A and B 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)). 解题思路:如果是一个已排序数组,求中位数,可以直接计算.因此比较常规的想法是直接将两个有序数组合并

【LeetCode】Median of Two Sorted Arrays (2 solutions)

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 arrays. The overall run time complexity should be O(log (m+n)). 解法一:保底做法,O(m+n)复杂度 按照归并排序的思路,数到median,然后计算返回. 需要注意: 如果是m+n

leetcode之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 sorted arrays. The overall run time complexity should be O(log (m+n)). 首先假设数组A和B的元素个数都大于k/2,我们比较A[k/2-1]和B[k/2-1]两个元素,这两个元素分别表示A的