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

Posted joannae

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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
}

 

以上是关于4. Median of Two Sorted Arrays (二分法;递归的结束条件)的主要内容,如果未能解决你的问题,请参考以下文章

leetcode-4 Median of Two Sorted Arrays

4. Median of Two Sorted Arrays

Median of two Sorted Arrays

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

4.Median of Two Sorted Arrays

#Leetcode# 4. Median of Two Sorted Arrays