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)).

double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size) {
    int len = nums1Size+nums2Size;
    int median = len >> 1;
    if(len%2==1) {
        if(nums1Size==0) return nums2[median];
        if(nums2Size==0) return nums1[median];
        return findK(nums1, 0, nums1Size, nums2, 0, nums2Size, median+1);
    }
    else{
        if(nums1Size==0) return (double)(nums2[median-1]+nums2[median])/2;
        if(nums2Size==0) return (double)(nums1[median-1]+nums1[median])/2;
        return (double)(findK(nums1, 0, nums1Size, nums2, 0, nums2Size, median)+findK(nums1, 0, nums1Size, nums2, 0, nums2Size, median+1))/2;
    }
}

int findK(int* nums1, int start1, int len1, int* nums2, int start2, int len2, int k){
    if(len1==1){ //check len = 1 case, because we keep median when recursion; otherwise, endless loop
        if(nums1[start1] < nums2[start2+k-2]) return nums2[start2+k-2];
        else{
            if(k > len2 || nums1[start1] < nums2[start2+k-1] ) return nums1[start1];
            else return nums2[start2+k-1];
        }
    }
    if(len2==1){
        if(nums2[start2] < nums1[start1+k-2]) return nums1[start1+k-2];
        else{
            if(k > len1 || nums2[start2] < nums1[start1+k-1] ) return nums2[start2];
            else return nums1[start1+k-1];
        }
    }

    int median1 = start1+(len1 >> 1); //if len is odd, it‘s exactly median; else if even, it‘s the second of the two median
    int median2 = start2+(len2 >> 1);

    if(k <= ((len1+len2)>>1)){ //k is at the first half
        if(nums1[median1] < nums2[median2]){ //delete the second half of nums2
            findK(nums1, start1, len1, nums2, start2, median2-start2, k); //1. delete from median (including median)
        }
        else{//delete the second half of nums1
            findK(nums1, start1, median1-start1, nums2, start2, len2, k);
        }
    }
    else{ //k is at the second half
        if(nums1[median1] < nums2[median2]){ //delete the first half of nums1
            findK(nums1, median1, len1-(median1-start1), nums2, start2, len2, k-(median1-start1)); //2. Each time delete half at most, so keep median
        }
        else{ //delete the first half of nums2
            findK(nums1, start1, len1, nums2, median2, len2-(median2-start2), k-(median2-start2));
        }
    }
    //From 1, 2, we can see, when only one element, it cannot be deleted, so the end loop condition is len = 1
}
时间: 2024-10-11 13:09:50

4. Median of Two Sorted Arrays (二分法;递归的结束条件)的相关文章

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 @ 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 | Median of Two Sorted Arrays 寻找2个有序数组中第k大的值

问题 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)). 分析 本题更经典通用的描述方式时: 给定2个有序数组,找出2个数组中所有元素中第k大的元素. 思路1 直观思

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)). 给定两个已

【算法导论学习-016】两个已排过序的等长数组的中位数(median of two sorted arrays)

问题来源 <算法导论>P223 9.3-8: Let X[1..n] and Y[1..n] be two arrays, each containing nnumbers already in sorted order. Give an O(lgn)-time algorithm to find themedian of all 2n elements in arrays X and Y. 翻译过来即:求两个等长(n个元素)的已排序数组A和B的中位数 方案1:对两个数组进行归并直到统计到第n

leetCode-002 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)). [题意] 有两个有序的数组,找出这两数组整合后的中位数,要求时间复杂度O(nlogn) [思路] 要找这两个排序数组的中位数,数组长度分别是Len(A)=m和Le

【经典】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为偶数时,此题的中位数是中间两个数的平均值. 分析: 1.令数组A始终为长度小

LeetCode OJ - 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)). 解题思路: 将原问题转变成一个寻找第k小数的问题(假设两个原序列升序排列),这样中位数实际上是第(m+n)/2小的数.所以只要解决了第k小数的问题,原问题也得以解决

LeetCode2: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为偶数的话,应为中间两数之和除2,但时间复杂度不符合题目要求,还一种方法就是利用归并思想,因