LeetCode 4.寻找两个有序数组的中位数

题目描述

给定两个大小为 m 和 n 的有序数组 nums1 和 nums2

请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n))。

你可以假设 nums1 和 nums2 不会同时为空。

示例 1:

nums1 = [1, 3]
nums2 = [2]

则中位数是 2.0

示例 2:

nums1 = [1, 2]
nums2 = [3, 4]

则中位数是 (2 + 3)/2 = 2.5

题解

方法一:归并法

思路

数组是有序的,则可以采用归并的思想:假设两个数组的规模为(m+n),每次从两个数组中取出剩下元素中较小的一个,一直取到第(m+n+1)/2次。若(m+n)为奇数,则第(m+n+1)/2次所取的数即为答案;若(m+n)为偶数,则需要再取一个数,与第(m+n+1)/2次所取的数的平均值即为答案。

    public double findMedianSortedArrays(int[] nums1, int[] nums2) {
        int time = (nums1.length + nums2.length + 1)/2;
        int i=0, j=0;
        int res = 0;
        while(time > 0) {
            if(i >= nums1.length) {
                res = nums2[j++];
            } else if(j >= nums2.length) {
                res = nums1[i++];
            } else if(nums1[i] < nums2[j]) {
                res = nums1[i++];
            } else {
                res = nums2[j++];
            }
            time--;
        }
        if(totalNum % 2 == 1)
            return res;
        int res2 = 0;
        if(i >= nums1.length) {
            res2 = nums2[j];
        } else if(j >= nums2.length) {
            res2 = nums1[i];
        } else if(nums1[i] < nums2[j]) {
            res2 = nums1[i];
        } else {
            res2 = nums2[j];
        }
        return ((int)(res + res2))/2.0;
    }

复杂度分析

  • 时间复杂度:O(m+n) 。

  • 空间复杂度:O(1)。

方法二

利用中位数的性质:中位数左右两边的元素个数相等。

class Solution {
    public double findMedianSortedArrays(int[] nums1, int[] nums2) {
        int[] a, b;
        if(nums1.length <= nums2.length) {
            a = nums1;
            b = nums2;
        } else {
            a = nums2;
            b = nums1;
        }
        int m = a.length;
        int n = b.length;
        int low = 0;
        int high = m;
        int i, j;
        while(low <= high) {
            i = (low + high)/2;
            j = (m + n + 1)/2-i;
            if(i > low && a[i-1] > b[j])
                high = i - 1;
            else if(i < high && b[j-1] > a[i])
                low = i + 1;
            else {
                int maxLeft;
                if(i == 0)
                    maxLeft = b[j-1];
                else if(j == 0)
                    maxLeft = a[i-1];
                else
                    maxLeft = Math.max(a[i-1], b[j-1]);
                if((m+n)%2 == 1)
                    return maxLeft;
                
                int minRight;
                if(i == m)
                    minRight = b[j];
                else if(j == n)
                    minRight = a[i];
                else
                    minRight = Math.min(a[i], b[j]);
                return (maxLeft + minRight)/2.0;
            }
        }
        return 0.0;
    }
}

复杂度分析

  • 时间复杂度:O(log(m+n)) 。

  • 空间复杂度:O(1)。

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值