[编程题]median-of-two-sorted-arrays
median-of-two-sorted-arrays
http://www.nowcoder.com/questionTerminal/82c11b9392b14f3abfbf257f79a76025
双指针解决
class Solution {
public:
double findMedianSortedArrays(int A[], int m, int B[], int n) {
int p1 = 0, p2 = 0;
int left = -1, right = -1;
for (int i = 0; i < (m + n) / 2 + 1; i ++){
left = right;
// p2 右移n
if (p1 >= m || p2 < n && A[p1] > B[p2]){
right = B[p2++];
}
// p1 右移
else {
right = A[p1++];
}
}
return (m + n) % 2 == 1 ? right : (left + right) / 2.0;
}
};