类似归并排序的合并阶段;
本来想直接用一个pre和now,然后每次用k++,加到一般时直接出结果,后来觉得麻烦还是用vector了;
class Solution {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
int i = 0, j = 0;
vector<int> result;//把nums1,nums2归并到result
int length = nums1.size() + nums2.size();
for (; i < nums1.size() && j < nums2.size();)
{
if(nums1[i]>nums2[j])
{
result.push_back(nums2[j++]);
}
else
{
result.push_back(nums1[i++]);
}
}
while (i<nums1.size())
{
result.push_back(nums1[i++]);
}
while (j<nums2.size())
{
result.push_back(nums2[j++]);
}
if(length%2==0 )
{
return result[length/2-1]/2.0+result[length/2]/2.0;
}
if(length%2==1 )
{
return result[length/2];
}
return 0;
}
};