Skip to content

Commit a27d247

Browse files
authored
Merge pull request #30 from KongJHong/master
Add solution 004[CPP]
2 parents decdb6c + 80adad0 commit a27d247

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class Solution {
2+
public:
3+
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
4+
int nums[10000] = { 0 };
5+
int index = 0;
6+
vector<int>::iterator it1 = nums1.begin();
7+
vector<int>::iterator it2 = nums2.begin();
8+
for (; it1 != nums1.end() && it2 != nums2.end();) {
9+
if (*it1 >= *it2) {
10+
nums[index++] = *it2;
11+
it2++;
12+
}
13+
else {
14+
nums[index++] = *it1;
15+
it1++;
16+
}
17+
}
18+
19+
while (it1 != nums1.end()) {
20+
nums[index++] = *it1;
21+
it1++;
22+
}
23+
while (it2 != nums2.end()) {
24+
nums[index++] = *it2;
25+
it2++;
26+
}
27+
28+
if (index % 2 == 0) {
29+
return (double)((nums[index/2] + nums[index/2 - 1])/2.0);
30+
}
31+
else {
32+
return (double)(nums[index/2]);
33+
}
34+
35+
}
36+
};

0 commit comments

Comments
 (0)