-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path4_Median_Of_Two_Sorted_Arrays.cpp
86 lines (80 loc) · 1.96 KB
/
4_Median_Of_Two_Sorted_Arrays.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <iostream>
#include <vector>
class Solution {
public:
double findMedianSortedArrays(std::vector<int>& nums1, std::vector<int>& nums2)
{
size_t len1 = nums1.size();
size_t len2 = nums2.size();
size_t mid = (len1 + len2) >> 1;
double res = 0, pre = 0;
size_t i=0, j=0, k=0;
for(; k<=mid; k++)
{
pre = res;
if(i<len1 && j<len2)
{
if(nums1.at(i) < nums2.at(j))
{
res = nums1.at(i++);
}
else
{
res = nums2.at(j++);
}
continue;
}
if(i<len1)
{
res = nums1.at(i++);
continue;
}
if(j<len2)
{
res = nums2.at(j++);
continue;
}
}
if(((len1 + len2) & 1) == 0)
{
res = (pre + res) / 2;
}
return res;
}
void ConstructFromArray(int arr[], size_t len, std::vector<int>& nums)
{
for(unsigned i=0; i<len; i++)
{
nums.push_back(arr[i]);
}
}
void Print(std::vector<int>& nums)
{
for(auto it : nums)
{
std::cout << "->" << it;
}
std::cout << std::endl;
}
void test(int arr1[], size_t len1, int arr2[], size_t len2)
{
std::vector<int> nums1;
ConstructFromArray(arr1, len1, nums1);
std::vector<int> nums2;
ConstructFromArray(arr2, len2, nums2);
std::cout << "array1";
Print(nums1);
std::cout << "array2";
Print(nums2);
double res = findMedianSortedArrays(nums1, nums2);
std::cout << "result:" << res << std::endl;
}
};
int main()
{
int arr1[] = {1,3};//1,3
int arr2[] = {2};//2
Solution example;
example.test(arr1, 2, arr2, 1);
return 0;
}