Skip to content

Commit da264ca

Browse files
committed
Median of Two Sorted Arrays
1 parent e92af89 commit da264ca

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

4.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
class Solution {
2+
public:
3+
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
4+
int m = nums1.size();
5+
int n = nums2.size();
6+
7+
int C[m+n];
8+
int i = 0;
9+
int j = 0;
10+
int k = 0;
11+
12+
while(i < m && j < n)
13+
{
14+
if(nums1[i] <= nums2[j])
15+
{
16+
C[k++] = nums1[i++];
17+
}
18+
else
19+
{
20+
C[k++] = nums2[j++];
21+
}
22+
}
23+
24+
if(i == m)
25+
{
26+
while( j < n )
27+
{
28+
C[k++] = nums2[j++];
29+
}
30+
}
31+
else
32+
{
33+
while( i < m )
34+
{
35+
C[k++] = nums1[i++];
36+
}
37+
}
38+
39+
k = n + m;
40+
if( k % 2 == 0)
41+
{
42+
return (C[k/2] + C[k/2 - 1])/2.0;
43+
}
44+
else
45+
{
46+
return C[k/2];
47+
}
48+
}
49+
};

0 commit comments

Comments
 (0)