leetcode个人题解——#4 Median of Two Sorted Arrays

题目描述:寻找两个有序数组合并后的中位数,要求算法时间复杂度为O(log(m+n))

参考官方题解。

说到中位数,两个序列合并后的中位数下标一定是m+n+1/2或中间两位数的平均数。

我们只需要不断地划分两个序列直到找到答案即可,划分序列可以用二分法,

    left_part                |  right_part
    A[0], A[1], ..., A[i-1]  |  A[i], A[i+1], ..., A[m-1]
    B[0], B[1], ..., B[j-1]  |  B[j], B[j+1], ..., B[n-1]

用二分法寻找i,由于左右两部分数量一定相同,所以j = (m + n + 1) / 2 - i.

且划分后序列要满足右边部分要恒大于左边部分,即B[j]>=A[j-1]且B[j-1]<=A[i]。

这是满足条件的答案,还有边界情况,比如i = 0或j = n。

搜索中会遇到三种情况:

(j=0 or i = m or B[j−1]≤A[i]) 或是 (i=0 orj=n or A[i−1]≤B[j]),这意味着 i 是完美的,我们可以停止搜索。
 i < m and B[j−1]>A[i] 这意味着 i 太小,我们必须增大它。
j<n and A[i−1]>B[j] 这意味着 i 太大,我们必须减小它。

代码:

class Solution {
public:
    double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
        int m = nums1.size();
        int n = nums2.size();
        if(m > n) {//这里为了让搜索次数最小
            nums1.swap(nums2);
            int res = m; m = n; n = res;
        }
        int iMin = 0;int iMax = m; int mid = (m + n + 1) / 2;
        while(iMin <= iMax){
            int i = (iMax + iMin) / 2;
            int j = mid - i;
            if(i < iMax && nums2[j - 1] > nums1[i]){
                iMin = i + 1;
            }
            else if(i > iMin && nums2[j] < nums1[i - 1]){
                iMax = i - 1;
            }
            else{
                int ans = 0;
                if(i == 0){ans = nums2[j - 1];}
                else if(j == 0) ans = nums1[i - 1];
                else ans = max(nums1[i - 1], nums2[j - 1]);
                if((m + n) % 2 == 1) return ans;
                int ans2 = 0;
                if(i == m) ans2 = nums2[j];
                else if(j == n) ans2 = nums1[i];
                else ans2 = min(nums1[i], nums2[j]);
                return (ans + ans2) / 2.0;
            }
        }
        return 0.0;
    }
};

原文地址:https://www.cnblogs.com/txltxl22/p/11300228.html

时间: 2024-10-10 04:22:55

leetcode个人题解——#4 Median of Two Sorted Arrays的相关文章

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【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 OJ 004】Median of Two Sorted Arrays

题目链接:https://leetcode.com/problems/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)). 解题思路:将两个有序数

LeetCode第四题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: nums1 = [1, 3] nums2 = [2] The median is 2.0 简单来说,这道题的要求就是求两个数组的中位数,因

4. Median of Two Sorted Arrays(topK-logk)

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 me

【题解】【数组】【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(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][Python]Median of Two Sorted Arrays

# -*- coding: utf8 -*-'''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)).