Try   HackMD

4. Median of Two Sorted Arrays

hard

Solution

double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size){ int two; int myarraysize=0; two = (nums1Size+nums2Size)%2?1:2; // count of calulation myarraysize =((nums1Size+nums2Size)>>1)+1; int i=0,j=0; int * myarray = calloc(myarraysize,sizeof(int)); double ans = 0.0; while((i+j)<=myarraysize-1){ if((j==(nums2Size))||(nums2==0)){ myarray[i+j] = nums1[i]; i++; } else if((i==(nums1Size))||(nums1==0)) { myarray[i+j] = nums2[j]; j++; } else if((nums1[i]>nums2[j])){ myarray[i+j] = nums2[j]; j++; } else{ myarray[i+j] = nums1[i]; i++; } } if(two==1) ans = (double)myarray[myarraysize-1]; else ans = (((double)myarray[myarraysize-1])+((double)myarray[myarraysize-2]))/2; return ans; }

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →