取自:https://leetcode-cn.com/problems/median-of-two-sorted-arrays/comments/
double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size) {
int k = 0;
int i = 0, j = 0;
int temp1 = 0, temp2 = 0;
if((nums1Size == 0) && (nums2Size == 0))
return 0;
if((nums1Size + nums2Size) % 2 == 1)
k = (nums1Size + nums2Size) / 2;
else
k = (nums1Size + nums2Size) / 2;
if(nums1Size == 0)
{
if((nums2Size) % 2 == 1)
return nums2[nums2Size / 2];
else
return (nums2[nums2Size / 2 - 1] + nums2[nums2Size / 2]) / 2.0;
}
else if (nums2Size == 0)
{
if((nums1Size) % 2 == 1)
return nums1[nums1Size / 2];
else
return (nums1[nums1Size / 2 - 1] + nums1[nums1Size / 2]) / 2.0;
}
else
{
while((i + j) <= k)
{
if ((i + j) % 2 == 0)
{
if((i < nums1Size) && (j < nums2Size))
(nums1[i] >= nums2[j]) ? (temp1 = nums2[j], j++) : (temp1 = nums1[i], i++);
else if(i < nums1Size)
temp1 = nums1[i], i++;
else if(j < nums2Size)
temp1 = nums2[j], j++;
}
else
{
if((i < nums1Size) && (j < nums2Size))
(nums1[i] >= nums2[j]) ? (temp2 = nums2[j], j++) : (temp2 = nums1[i], i++);
else if(i < nums1Size)
temp2 = nums1[i], i++;
else if(j < nums2Size)
temp2 = nums2[j], j++;
}
}
if((nums1Size + nums2Size) % 2 == 1)
return (temp1 > temp2) ? temp1 : temp2;
else
return (temp1 + temp2) / 2.0;
}
}