标签:style http color 使用 os io ar div
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) { int i = 0, j = 0, k = 0; double r; int* result = new int[m + n]; while( i < m && j < n ) { if( A[i] < B[j] ) { result[k++] = A[i++]; } else { result[k++] = B[j++]; } } while( i < m ) { result[k++] = A[i++]; } while( j < n ) { result[k++] = B[j++]; } if( 1 == (m + n)%2 ) { r = result[(m + n - 1) / 2]; } else { r = (double)( result[(m + n - 2) / 2] + result[(m + n) / 2] ) / 2; } delete [] result; return r; } };
【Leet Code】Median of Two Sorted Arrays,布布扣,bubuko.com
【Leet Code】Median of Two Sorted Arrays
标签:style http color 使用 os io ar div
原文地址:http://blog.csdn.net/u012771236/article/details/38728017