LeetCode4 Median of Two Sorted Arrays Java实现

描述:

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(log (m+n)).

如果把两个数组合并,排序再找中位数,显然时间复杂度是O(nlogn).

问题转换为求两个数组中第K个小的元素.参考http://blog.csdn.net/yutianzuijin/article/details/11499917/

首先假设数组A和B的元素个数都大于k/2,我们比较A[k/2-1]和B[k/2-1]两个元素,这两个元素分别表示A的第k/2小的元素和B的第k/2小的元素。这两个元素比较共有三种情况:>、<和=。如果A[k/2-1]<B[k/2-1],这表示A[0]到A[k/2-1]的元素都在A和B合并之后的前k小的元素中。换句话说,A[k/2-1]不可能大于两数组合并之后的第k小值,所以我们可以将其抛弃。

当A[k/2-1]>B[k/2-1]时存在类似的结论。

当A[k/2-1]=B[k/2-1]时,我们已经找到了第k小的数,也即这个相等的元素.

  • 如果A或者B为空,则直接返回B[k-1]或者A[k-1];
  • 如果k为1,我们只需要返回A[0]和B[0]中的较小值;
  • 如果A[k/2-1]=B[k/2-1],返回其中一个;

public class Solution {
    public static double findKth(int a[],int begina,int m,int b[],int beginb,int n,int k){
        if(m>n)              //确保函数m<=n
            return findKth(b,beginb,n,a,begina,m,k);
        if(m==0)
            return b[beginb+k-1];
        if(k==1)
            return Math.min(a[begina],b[beginb]);
        int ma = Math.min(k / 2, m), mb = k - ma;  //把k分成两部分
        if(a[begina+ma-1]<b[beginb+mb-1])        //把a数组前面ma个元素去掉,第k小的元素不在这里
            return  findKth(a,begina+ma,m-ma,b,beginb,n,k-ma);
        else if(a[begina+ma-1]>b[beginb+mb-1])   //把b数组前面mb个元素去掉,第k小的元素不在这里
            return  findKth(a,begina,m,b,beginb+mb,n-mb,k-mb);
        else return a[begina+ma-1];              //相等时就是它

    }
    public static double findMedianSortedArrays(int A[], int B[]) {
        int m=A.length;
        int n=B.length;
        int totalLength=m+n;
        if (totalLength%2 == 1)      //奇数长度
            return findKth(A, 0, m, B, 0, n, totalLength/2+1);
        else {                           //偶数长
            return (findKth(A, 0, m, B, 0, n, totalLength / 2) + findKth(A, 0, m, B, 0, n, totalLength / 2 + 1)) / 2;
        }
    }
    public static void main(String[] args){
        int[] a={3};
        int[] b={1,2};
        double median=findMedianSortedArrays(a,b);
        System.out.println(median);

    }
}

View Java Code

http://blog.csdn.net/yutianzuijin/article/details/11499917/

时间: 2024-10-14 03:30:52

LeetCode4 Median of Two Sorted Arrays Java实现的相关文章

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)). 给定两个已

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,

Median of Two Sorted Arrays(Java)

求2个数组的中位数 方法很多 但是时间复杂度各异 1利用数组copy方法先融合两个数组,然后排序,找出中位数 import java.lang.reflect.Array; import java.util.Arrays; import java.util.Collection; import java.util.Collections; public class _004MedianofTwoSortedArrays { public static void main(String[] arg

leetcode4. Median of Two Sorted Arrays

题目大意:找到两个排序数组的中位数. 水了一发,将两个数组合并为一个数组,直接输出中位数了.(可见leetcode好像并没有对时间进行控制). public class Solution { public double findMedianSortedArrays(int[] nums1, int[] nums2) { int []num = new int [nums1.length+nums2.length]; int i; for(i = 0;i < nums1.length;i++) n

4. Median of Two Sorted Arrays【leetcode】java,算法,中间值

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 sorted arrays. The overall run time complexity should be O(log (m+n)). Example 1: nums1 = [1, 3] nums2 = [2] The media

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

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,大小为m 和 n. 找出两数组的中位数 时间复杂度应该为 O(log (m+n)). 简单粗暴的方法就是把两个数组合并成一个数组,排序,取中位数.

第三周 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)). 解题思路:如果是一个已排序数组,求中位数,可以直接计算.因此比较常规的想法是直接将两个有序数组合并