There are two sorted arrays nums1 and nums2 of
size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).
题目翻译:
给定两个已经排序过的数组,求这两个数组中的中位数,要求时间复杂度为O(log (m+n)).
<span style="font-size:14px;">#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2)
{
int n = nums1.size();
int m = nums2.size();
vector<int> num3;
num3.insert(num3.end(),nums1.begin(),nums1.end());
num3.insert(num3.end(),nums2.begin(),nums2.end());
sort(num3.begin(),num3.end());
int k = n+m;
double target = (double) (k%2?num3[k/2]:(num3[k/2]+num3[k/2-1])/2.0);
return target;
}
int main()
{
int a[] = {3,7,8};
int b[] = {2,6,9};
vector<int> s1(a,a+3);
vector<int> s2(b,b+3);
double tar = findMedianSortedArrays(s1,s2);
cout<<tar<<endl;
return 0;
}</span>