Use 4 position index to exclude redundant number firstly


  • 0
    C
    double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size) {
        if(nums1Size>nums2Size) return findMedianSortedArrays(nums2,nums2Size,nums1,nums1Size);
    int lpos=0,rpos=nums1Size-1,lpos2=0,rpos2=nums2Size-1;
    if(nums1Size==0) rpos--;
    if(nums1Size==1&&nums2Size==1) return ((double)nums1[0] + (double)nums2[0])/2;
    while(lpos<=rpos&&lpos2<=rpos2){
        if(nums1[lpos]<=nums2[lpos2]){
            lpos++;
        }
        else lpos2++;
        if(nums1[rpos]>=nums2[rpos2]){
            rpos--;
        }
        else rpos2--;
        if(lpos>rpos||lpos2>rpos2) break;
        if(lpos==rpos&&lpos2==rpos2) return ((double)nums2[rpos2] + (double)nums1[rpos])/2;
    }
    if(lpos>rpos){
        return ((double)nums2[(rpos2+lpos2)/2] + (double)nums2[(rpos2+lpos2+1)/2])/2;
    }
    else if(lpos2>rpos2)
    return ((double)nums1[(rpos+lpos)/2] + (double)nums1[(rpos+lpos+1)/2])/2;
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.