第四题:Median of Two Sorted Arrays

题目链接:题目链接

题意:两个排好序的数组,找到中位数,如果是奇数好办,如果是偶数找到最中间的两个求平均值。

这一题的本质其实就是第mid小的数。

这一题看到一种好的方法,我们令k=mid,对于两个数组,分别取前k/2数,如果A[k/2-1]比B[k/2-1]大,那么说明B前
k/2数肯定在k小数中,A的则不一定,则下面需要从A,B+k/2再次去寻找。。。反之对于B大的情况也是一样的,如果相
等,那就是这两个数(相等)了,随便返回一个都OK。(Ps:解释一些为什么是k/2-1,因为k是从1开始取,数组下标从0开
始)。

代码如下:

class Solution {
public:
    // 参数K:从1开始
    //
    double dooo(int A[], int m, int B[], int n, int k){
        // 为了方便处理,默认m比n小
        //
        if (m > n)
            return dooo(B, n, A, m, k);

        // 如果数组A没有元素了,那么返回B中k-1就OK
        //
        if (!m)
            return B[k-1];

		if (k==1)
			return min(A[0], B[0]);

        // A中前面k/2数,如果比m大,那么取m
        // 对于b,那么和a_idx和是k
        //
        int a_idx = min(k/2, m);
        int b_idx = k - a_idx;

        // 如果A比B值大,说明B中前b_idx数都在K小数中
        // 反之一样
        //
        if (A[a_idx-1] > B[b_idx-1])
            // 如果B前b_idx数都在前k小数中,那么下面从A和B+b_idx再次进行递归
            //
            return dooo(A, m, B+b_idx, n-b_idx, k-b_idx);
        else if (A[a_idx-1] < B[b_idx-1])
            return dooo(A+a_idx, m-a_idx, B, n, k-a_idx);
        else
            return A[a_idx-1];
    }

    double findMedianSortedArrays(int A[], int m, int B[], int n) {
        // 如果是偶数,需要返回中间两个数,获得平均数
        // 注意第三个参数k从1开始,所以需要+1
        //
        if ((n + m) % 2 == 0)
            return (dooo(A, m, B, n, (m+n)/2) + dooo(A, m, B, n, (m+n)/2+1)) / 2.0;
        else
            // 否则中间数就OK
            return dooo(A, m, B, n, (m+n)/2+1);
    }
};
时间: 2024-08-28 11:30:35

第四题:Median of Two Sorted Arrays的相关文章

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 简单来说,这道题的要求就是求两个数组的中位数,因

Python语言,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 1: nums1 = [1, 3] nums2 = [2] The median is 2.0 Example 2: nums1 = [1,

LeetCode-刷题 Median of Two Sorted Arrays

之前一直是写C++的,想着开始学学用JAVA写Code会有什么不同,是否会更加简洁?于是开始学着用JAVA去刷LEETCODE 习题如下: 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

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

题目再现 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)). 这道题的

周刷题第二期总结(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

刷题4. Median of Two Sorted Arrays

一.题目 Median of Two Sorted Arrays,具体请自行搜索. 这个题目,我看了一下,经过一番思考,我觉得实现起来不是很复杂. 但要做到bug free也不难,最大的问题是性能问题. 性能只有42%的样子,内存占用太多.还需要进一步优化!!! 二.这个题目,我自己实现 提交了2次: 第1次: Wrong Answer 第2次:终于对了 下面是我的完整代码实现,需要的拿去: #include<iostream> #include<vector> using nam

[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)). 解题思路:这道题要求两个已经排好

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

[LintCode] 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. Have you met this question in a real interview? Example Given A=[1,2,3,4,5,6] and B=[2,3,4,5], the median is 3.5. Given A=[1,2,3] and B=[4,5],