Repository navigation
Expand file tree
/
Copy path004_MedianOfTwoSortedArrays.cpp
More file actions
46 lines (44 loc) · 1.69 KB
/
Copy path004_MedianOfTwoSortedArrays.cpp
File metadata and controls
46 lines (44 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/**
* 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
*/
class Solution {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
int len1 = nums1.size(), len2 = nums2.size();
int k = (len1 + len2) / 2 + 1;
if((len1 + len2) & 0x1)
return find_Kth_InSortedArrays(nums1.begin(), nums2.begin(), len1, len2, k);
else
return ( find_Kth_InSortedArrays(nums1.begin(), nums2.begin(), len1, len2, k - 1)
+ find_Kth_InSortedArrays(nums1.begin(), nums2.begin(), len1, len2, k ) ) / 2.0;
}
int find_Kth_InSortedArrays(vector<int>::const_iterator pLeft, vector<int>::const_iterator pRight, int lLen, int rLen, int k)
{
if(k < 1 || k > lLen + rLen)
return -1;
if(lLen > rLen)
return find_Kth_InSortedArrays(pRight, pLeft, rLen, lLen, k);
if(0 == lLen)
return *(pRight + k - 1);
if(1 == k)
return std::min(*pLeft, *pRight);
int lMid = std::min(k >> 1, lLen);
int rMid = k - lMid;
if(*(pLeft + lMid - 1) == *(pRight + rMid - 1))
return *(pLeft + lMid - 1);
else if(*(pLeft + lMid - 1) < *(pRight + rMid - 1))
return find_Kth_InSortedArrays(pLeft + lMid, pRight, lLen - lMid, rLen, k - lMid);
else
return find_Kth_InSortedArrays(pLeft, pRight + rMid, lLen, rLen - rMid, k - rMid);
}
};