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++)
        num[i] = nums1[i];

        for(int j = 0;j < nums2.length;j++)
        num[i++] = nums2[j];

        int m = num.length;
        Arrays.sort(num);

        if(num.length == 0) return 0;

        if(num.length == 1) return num[0];

        if(m % 2 == 0)
        return (double)(num[m/2-1] + num[m/2])/2;
        else
        return (double)num[m/2];

    }
}

另一种方法:

该问题换成另一种说法就是:寻找两个数组中第K小的数字,这里K=(m+n)/ 2。

首先假设数组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]大于合并之后的第k小值,我们不妨假定其为第(k+1)小值。由于A[k/2-1]小于B[k/2-1],所以B[k/2-1]至少是第(k+2)小值。但实际上,在A中至多存在k/2-1个元素小于A[k/2-1],B中也至多存在k/2-1个元素小于A[k/2-1],所以小于A[k/2-1]的元素个数至多有k/2+ k/2-2,小于k,这与A[k/2-1]是第(k+1)的数矛盾。

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

当A[k/2-1]=B[k/2-1]时,我们已经找到了第k小的数,也即这个相等的元素,我们将其记为m。由于在A和B中分别有k/2-1个元素小于m,所以m即是第k小的数。(这里可能有人会有疑问,如果k为奇数,则m不是中位数。这里是进行了理想化考虑,在实际代码中略有不同,是先求k/2,然后利用k-k/2获得另一个数。)

通过上面的分析,我们即可以采用递归的方式实现寻找第k小的数。此外我们还需要考虑几个边界条件:

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

(原文分析:  http://blog.csdn.net/zxzxy1988/article/details/8587244

        /*int n = nums1.length+nums2.length;
        if(n % 2 != 0)
        return find_median(nums1,nums1.length,nums2,nums2.length,n/2+1);
        else
        return (find_median(nums1,nums1.length,nums2,nums2.length,n/2)+find_median(nums1,nums1.length,nums2,nums2.length,n/2+1))/2;

    }

    public int find_median(int a[],int m,int b[],int n,int k)
    {
        if(m > n) return find_median(b,n,a,m,k);
        if(m == 0) return b[k-1];
        if(k == 1) return min(a[0],b[0]);

        int pa = min(k/2,m), pb = k - pa;
        if(a[pa-1] < b[pb-1])
        return find_median(a+pa,m-pb,b,n,k-pa);

        else if (a[pa-1] > b[pb-1])
        return find_median(a,m,b+pb,n-pb,k-pb);

        else
        return a[pa-1];
    }

    public int min(int a, int b)
    {
        if(a < b) return a;
        else return b;
    }

在最好情况下,每次都有k一半的元素被删除,所以算法复杂度为logk,由于求中位数时k为(m+n)/2,所以算法复杂度为log(m+n)。

时间: 2024-08-10 20:53:11

leetcode4. Median of Two Sorted Arrays的相关文章

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). 问题转换为求两个数组

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

【题解】【数组】【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

周刷题第二期总结(Longest Substring Without Repeating Characters and Median of Two Sorted Arrays)

这周前面刷题倒是蛮开心,后面出了很多别的事情和问题就去忙其他的,结果又只完成了最低目标. Lonest Substring Without Repeating Characters: Given a string, find the length of the longest substring without repeating characters. Examples: Given "abcabcbb", the answer is "abc", which t

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)). 题意解析 题目意思是给两个大小为m,n的有序数组(m,n可能为0),要求找出这两个数组的中位数.并且程序的时间复杂度必须不能超过O(log(m+n)). 这道题的

[leetcode]Median of Two Sorted Arrays @ Python

原题地址:https://oj.leetcode.com/problems/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)). 解题思路:这道题要求两个已经排好