19ms on c,shared my solution.


  • -1
    B

    double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size)
    {
    int *c;
    c = (int *)malloc((nums1Size+nums2Size)*sizeof(int));
    int i,j,k1=0,k2=0;
    double d;
    for(i=0;i<nums1Size;i++)
    {
    c[i]=nums1[i];
    }
    for(j=0;j<nums2Size;j++,i++)
    {
    c[i]=nums2[j];
    }

    int cmp ( const void *a , const void *b )
    {
    return *(int *)a - *(int *)b;
    }
    qsort(c,(nums1Size+nums2Size),sizeof(c[0]),cmp);
    if((nums1Size+nums2Size)%2==1)
     {
         k1=(nums1Size+nums2Size-1)/2;
         
         return c[k1];
         
     }
    else
    {
         k1=(nums1Size+nums2Size)/2;
         k2=(nums1Size+nums2Size-2)/2;
         d=(double)(c[k1]+c[k2])/2;
         return d;
    }
    

    }


Log in to reply
 

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