-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMedian of Two Sorted Arrays.cpp
72 lines (62 loc) · 1.23 KB
/
Median of Two Sorted Arrays.cpp
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
//按照归并排序,先将两个有序数组合并,然后找合并后数组的中位数//
//复杂度O(m+n),m,n分别代表两个数组的长度
#include<iostream>
#include<vector>
using namespace std;
class Solution
{
public:
double findMedianSortedArrays(vector<int> & nums1, vector<int> & nums2)
{
int m = nums1.size();
int n = nums2.size();
vector<int> merged_array = merge(nums1, nums2);
return (merged_array[(m + n) / 2] + merged_array[(m + n - 1) / 2]) / 2.0; //一个亮点,不用考虑数组长度的奇偶性
}
vector<int> merge(const vector<int> A, const vector<int> B)
{
vector<int> result;
int m = A.size(), n = B.size();
int i = 0, j = 0;
while (i != m && j != n)
{
if (A[i] <= B[j])
{
result.push_back(A[i]);
i++;
}
else
{
result.push_back(B[j]);
j++;
}
}
if (i == m)
{
while (j != n)
{
result.push_back(B[j]);
j++;
}
}
if (j == n)
{
while (i != m)
{
result.push_back(A[i]);
i++;
}
}
return result;
}
private:
};
int main()
{
vector<int> ivec1{ 1,3,5,7 };
vector<int> ivec2{ 2,5,6,8 };
Solution solution;
double result = solution.findMedianSortedArrays(ivec1, ivec2);
cout << result << endl;
return 0;
}