-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.h
62 lines (49 loc) · 1.17 KB
/
Solution.h
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#pragma once
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
int m, n, tmp;
vector<int> temp;
m = nums1.size(); n = nums2.size();
if (m > n)
{
temp = nums1; nums1 = nums2; nums2 = temp;
tmp = m; m = n; n = tmp;
}
if (n == 0) 0.0;
int i, j, mid;
int imin, imax, left_max, right_min;
imin = 0; imax = m;
mid = (n + m + 1) / 2;
while (imin <= imax)
{
i = (imin + imax) / 2;
j = mid - i;
if (i < m && nums2[j - 1] > nums1[i])
{
// i is too small
imin = i + 1;
}
else if (i > 0 && nums1[i - 1] > nums2[j])
{
// i is too big
imax = i - 1;
}
else {
// i is perfect
if (i == 0) left_max = nums2[j - 1];
else if (j == 0) left_max = nums1[i - 1];
else left_max = max(nums1[i - 1], nums2[j - 1]);
if ((n + m) % 2 == 1) return left_max;
if (i == m) right_min = nums2[j];
else if (j == n) right_min = nums1[i];
else right_min = min(nums1[i], nums2[j]);
return (left_max + right_min) / 2.0;
}
}
return 0.0;
}
};