标签:2.0 span dia bsp ott int amp The question
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)).
Example 1:
nums1 = [1, 3] nums2 = [2] The median is 2.0
Example 2:
nums1 = [1, 2] nums2 = [3, 4] The median is (2 + 3)/2 = 2.5
double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size) { int num3size = nums1Size + nums2Size; int *nums3 = (double*)malloc(num3size *sizeof(double)); int i=0,j=0,k=0; double result; for(i=0, j=0, k=0; i<num3size; i++) { if(j<nums1Size && k<nums2Size) { if(nums1[j]<nums2[k]) { nums3[i]=nums1[j]; j++; } else { nums3[i]=nums2[k]; k++; } } else { if(j<nums1Size) { nums3[i]=nums1[j]; j++; } if(k<nums2Size) { nums3[i]=nums2[k]; k++; } } } if(num3size%2==0) { result = (nums3[num3size/2-1]+nums3[num3size/2])/2.0; } else { result = nums3[(num3size-1)/2]; } free(nums3); return result; }
4. Median of Two Sorted Arrays
标签:2.0 span dia bsp ott int amp The question
原文地址:https://www.cnblogs.com/anne89/p/9089925.html