-
Notifications
You must be signed in to change notification settings - Fork 0
/
4.cpp
52 lines (47 loc) · 1.01 KB
/
4.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
// Time: O(log(m + n)): http://blog.csdn.net/yutianzuijin/article/details/11499917
// Time: O(m + n)
class Solution {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
int m = nums1.size();
int n = nums2.size();
int C[m+n];
int i = 0;
int j = 0;
int k = 0;
while(i < m && j < n)
{
if(nums1[i] <= nums2[j])
{
C[k++] = nums1[i++];
}
else
{
C[k++] = nums2[j++];
}
}
if(i == m)
{
while( j < n )
{
C[k++] = nums2[j++];
}
}
else
{
while( i < m )
{
C[k++] = nums1[i++];
}
}
k = n + m;
if( k % 2 == 0)
{
return (C[k/2] + C[k/2 - 1])/2.0;
}
else
{
return C[k/2];
}
}
};