My C 13ms solution.


  • 0
    J

    although my solution is 13ms,i think it’s not reach the problem request.
    Could anybody tell me the algorithm which time complexity is O(log (m+n))??

    double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size) {
        
        int i =  0,j = 0;
        int *NewArray = (int *)malloc((nums1Size + nums2Size) * sizeof(int));
        int ArraySize = 0;
        int inx;
        double Result = 0;
       
        while (i < nums1Size && j < nums2Size) {
            if (nums1[i] < nums2[j]) {
                NewArray[ArraySize] = nums1[i];
                i ++;
                ArraySize ++;
            }else{
                NewArray[ArraySize] = nums2[j];
                j ++;
                ArraySize ++;
            }
        }
        
        while (i < nums1Size) {
            NewArray[ArraySize] = nums1[i];
            i ++;
            ArraySize ++;
        }
        
        while (j < nums2Size) {
            NewArray[ArraySize] = nums2[j];
            j ++;
            ArraySize ++;
        }
        
        if(ArraySize % 2 == 0){
            inx = ArraySize/2;
            Result = (NewArray[inx] + NewArray[inx - 1])*1.0 / 2;
        }else{
            inx = ArraySize/2;
            Result = NewArray[inx];
        }
    
        return Result;
    }
    

Log in to reply
 

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