There are two sorted arrays A and B 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)).
class Solution {
public:
double findMedianSortedArrays(int A[], int m, int B[], int n)
{
if(!m && !n)
return 0;
int *a = new int[m+n];
memcpy(a, A, sizeof(int)*m);
memcpy(a+m, B, sizeof(int)*n);
sort(a, a+m+n);
int k = m + n;
double mid = k % 2 == 1 ? a[k>>1] : (a[k>>1] + a[(k-1)>>1])/2.0;
delete []a;
return mid;
}
};