-
Notifications
You must be signed in to change notification settings - Fork 64
/
sol.cpp
53 lines (39 loc) · 1006 Bytes
/
sol.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
#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(m + n);
int i = 0;
int j = 0;
int k = 0;
while(i < m && j < n){
if(nums1[i] <= nums2[j]) {
merged[k++] = nums1[i++];
}
else {
merged[k++] = nums2[j++];
}
}
while(i < m) {
merged[k++] = nums1[i++];
}
while(j < n) {
merged[k++] = nums2[j++];
}
int mid = merged.size() / 2;
if(merged.size() % 2 == 0) {
return (double)(merged[mid] + merged[mid - 1]) / 2;
}
else {
return merged[mid];
}
}
};
int main() {
// call the fn here
return 0;
}