File tree Expand file tree Collapse file tree 1 file changed +36
-0
lines changed
solution/004.Median of Two Sorted Arrays Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Original file line number Diff line number Diff line change
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
+ };
You can’t perform that action at this time.
0 commit comments