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 two sorted arrays. The overall run time complexity should be O(log (m+n)).

解题思路:
思路1:最直接的一种思路就是将两个有序的数组合并nums3,然后从nums3里找出中位数。当然处理的时候会有一些细节,具体的处理请看代码。

思路1提交的代码:

public double findMedianSortedArrays(int[] nums1, int[] nums2) {
        double num = 0;
        int m = nums1.length;
        int n = nums2.length;
        if(nums2.length == 0){
            if(m % 2 == 0){
                num = (nums1[m /2 - 1] + nums1[m / 2 ]) / 2.0;
                System.out.println(num);
            }else{
                num = nums1[m  / 2];
            }
            return num;
        }
        if(nums1.length == 0){
            if(n % 2 == 0){
                num = (nums2[n /2 - 1] + nums2[n / 2 ]) / 2.0;
                System.out.println(num);
            }else{
                num = nums2[n  / 2];
            }
            return num;
        }
        int index1 = 0;
        int index2 = 0;
        int[] nums3 = new int[n + m];
        int k = 0;

        while(index1 < m && index2 < n ){
            if(nums1[index1] < nums2[index2]){
                nums3[k++] = nums1[index1++];
            }else{
                nums3[k++] = nums2[index2++];
            }
        }
        while(index1 < m){
            nums3[k++] = nums1[index1++];
        }
        while(index2 < n){
            nums3[k++] = nums2[index2++];
        }

        if((m + n) % 2 == 0){
            num = (nums3[(m + n)/2 - 1] + nums3[(m + n) / 2 ]) / 2.0;
        }else{
            num = nums3[(m + n) / 2];
        }
        return num;
    }

思路2:思路1存在的问题:我们在求中位数的时候根本不关系具体的数,关心的是数值在列表中的位置,试想我们仅仅对两个数组的前半部分(或者后半部分)进行合并,找到中位数的位置之后就停止,岂不是要节约时间。

思路2提交的代码:

    public double findMedianSortedArrays(int[] nums1, int[] nums2) {
        double num = 0;
        int m = nums1.length;
        int n = nums2.length;

        if(nums2.length == 0){
            if(m % 2 == 0){
                num = (nums1[m /2 - 1] + nums1[m / 2 ]) / 2.0;
                System.out.println(num);
            }else{
                num = nums1[m  / 2];
            }
            return num;
        }
        if(nums1.length == 0){
            if(n % 2 == 0){
                num = (nums2[n /2 - 1] + nums2[n / 2 ]) / 2.0;
                System.out.println(num);
            }else{
                num = nums2[n  / 2];
            }
            return num;
        }
        if(m + n == 2){
            num = (nums1[m - 1] + nums2[n - 1]) / 2.0;
            return num;
        }

        int index1 = 0;
        int index2 = 0;
        int cell =  (int) Math.floor((m + n)/2.0) + 1;
        int[] nums3 = new int[cell];
        int k = 0;
        while(index1 < m && index2 < n){
            if(nums1[index1] < nums2[index2]){
                nums3[k++] = nums1[index1++];
            }else{
                nums3[k++] = nums2[index2++];
            }
            if ( (index1 + index2) >= cell ) {
                break;
            }
        }
        while(index1 < m && (index1 + index2) < cell){
            nums3[k++] = nums1[index1++];
        }
        while(index2 < n && (index1 + index2) < cell){
            nums3[k++] = nums2[index2++];
        }

        if((m + n) % 2 == 0){
            num = (nums3[cell - 2 ] + nums3[cell - 1 ]) / 2.0;
        }else{
            num = nums3[cell - 1];
        }
        return num;
    }

从理论上讲,第二种方法对于大数据集来说会更加节约时间,但是从leetcode提交结果后的效果,第二种的时间不太理想。

可以使用二分查找算法来解决这个问题。 首先,我们可以将两个数组合并成一个有序数组,然后求出中位数。但是,这个方法的时间复杂度为 $O(m + n)$,不符合题目要求。因此,我们需要寻找一种更快的方法。 我们可以使用二分查找算法在两个数组中分别找到一个位置,使得这个位置将两个数组分成的左右两部分的元素个数之和相等,或者两部分的元素个数之差不超过 1。这个位置就是中位数所在的位置。 具体来说,我们分别在两个数组中二分查找,假设现在在第一个数组中找到了一个位置 $i$,那么在第二个数组中对应的位置就是 $(m + n + 1) / 2 - i$。如果 $i$ 左边的元素个数加上 $(m + n + 1) / 2 - i$ 左边的元素个数等于 $m$ 个,或者 $i$ 左边的元素个数加上 $(m + n + 1) / 2 - i$ 左边的元素个数等于 $m + 1$ 个,则这个位置就是中位数所在的位置。 具体的实现可以参考以下 Java 代码: ```java public double findMedianSortedArrays(int[] nums1, int[] nums2) { int m = nums1.length, n = nums2.length; if (m > n) { // 保证第一个数组不大于第二个数组 int[] tmp = nums1; nums1 = nums2; nums2 = tmp; int t = m; m = n; n = t; } int imin = 0, imax = m, halfLen = (m + n + 1) / 2; while (imin <= imax) { int i = (imin + imax) / 2; int j = halfLen - i; if (i < imax && nums2[j - 1] > nums1[i]) { imin = i + 1; // i 太小了,增大 i } else if (i > imin && nums1[i - 1] > nums2[j]) { imax = i - 1; // i 太大了,减小 i } else { // i 是合适的位置 int maxLeft = 0; if (i == 0) { // nums1 的左边没有元素 maxLeft = nums2[j - 1]; } else if (j == 0) { // nums2 的左边没有元素 maxLeft = nums1[i - 1]; } else { maxLeft = Math.max(nums1[i - 1], nums2[j - 1]); } if ((m + n) % 2 == 1) { // 总元素个数是奇数 return maxLeft; } int minRight = 0; if (i == m) { // nums1 的右边没有元素 minRight = nums2[j]; } else if (j == n) { // nums2 的右边没有元素 minRight = nums1[i]; } else { minRight = Math.min(nums1[i], nums2[j]); } return (maxLeft + minRight) / 2.0; } } return 0.0; } ``` 时间复杂度为 $O(\log\min(m, n))$。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值