4. 寻找两个正序数组的中位数
为保证权益,题目请参考 4. 寻找两个正序数组的中位数(From LeetCode).
解决方案1
CPP
C++
/*
4. 寻找两个有序数组的中位数
@TIme : 2020-02-22 15:12
@auther: Keven Ge
*/
#include <iostream>
#include <vector>
#include <algorithm>
#include <stdio.h>
using namespace std;
class Solution
{
public:
double findMedianSortedArrays(vector<int> &nums1, vector<int> &nums2)
{
int m = nums1.size();
int n = nums2.size();
if (m <= n)
{
int i, j;
int imin = 0;
int imax = m;
while (imin <= imax)
{
i = (imin + imax) / 2;
j = (m + n + 1) / 2 - i;
if (i != m && j != 0 && nums2[j - 1] >= nums1[i])
{
imin = i + 1;
}
else if (i != 0 && j != n && nums1[i - 1] > nums2[j])
{
imax = i - 1;
}
else
{
if ((m + n) % 2 == 0)
{
double nums_min = 0, // 低的最大值
nums_max = 0; // 高的最小值
if (i == 0)
{
nums_min = double(nums2[j - 1]);
if (i != m)
{
if (j != n)
{
nums_max = min(double(nums1[0]), double(nums2[j]));
}
else
{
nums_max = double(nums1[i]);
}
}
else
{
nums_max = double(nums2[j]);
}
}
else if (i == m)
{
if (j != 0)
{
nums_min = double(max(nums1[i - 1], nums2[j - 1]));
}
else
{
nums_min = nums1[i - 1];
}
nums_max = double(nums2[j]);
}
else
{
nums_min = double(max(nums1[i - 1], nums2[j - 1]));
nums_max = double(min(nums1[i], nums2[j]));
}
// cout << nums_min << endl;
// cout << nums_max << endl;
// cout << nums_max - nums_min << endl;
double sum = (nums_max - nums_min) / 2;
// cout << sum << endl;
sum = sum + nums_min;
// cout << sum << endl;
return sum;
}
else
{
double nums_min;
if (i)
{
nums_min = double(max(nums1[i - 1], nums2[j - 1]));
}
else
{
nums_min = double(nums2[j - 1]);
}
return nums_min;
}
}
}
}
else
{
return findMedianSortedArrays(nums2, nums1);
}
return -1;
}
};
int main()
{
vector<int> vec1, vec2;
vec1.push_back(100001);
//vec1.push_back(2);
// vec2.push_back(1);
vec2.push_back(100000);
//vec2.push_back(3);
Solution so;
printf("%f\n", so.findMedianSortedArrays(vec1, vec2));
//cout << 20001.5 << endl;
//printf("%f", 20000001.5);
return 0;
}
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
解决方案2
CPP
C++
#include <iostream>
#include <vector>
using namespace std;
class Solution
{
public:
double findMedianSortedArrays(vector<int> &nums1, vector<int> &nums2)
{
// start index of the list
int l1s = 0;
int l2s = 0;
// end index of the list
int l1e = nums1.size() - 1;
int l2e = nums2.size() - 1;
// mainloop
int k = (nums1.size() + nums2.size());
while (k != 1)
{
int k2 = k / 2;
if (l1s + k2 <= l1e && l2s + k2 <= l2e)
{
l1s = l1s + k2;
l2s = l2s + k2;
}
else if (l1s + k2 > l1e)
{
k2 = k - (l1e - l1s + 1);
l1s = l1e + 1;
l2s += k2;
}
}
}
};
int main()
{
return 0;
}
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
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