Given two sorted arrays nums1
and nums2
of size m
and n
respectively, return the median of the two sorted arrays.
The overall run time complexity should be /(O(/log (m+n))/).
Solution
简单排序即可:
点击查看代码
class Solution {
private:
vector<int> vc;
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
for(int i=0;i<nums1.size();i++)vc.push_back(nums1[i]);
for(int i=0;i<nums2.size();i++)vc.push_back(nums2[i]);
sort(vc.begin(),vc.end());
if(vc.size()==0)return 0;
if(vc.size()%2){
return 1.0*vc[(vc.size()-1)/2];
}
else{
return 0.5*(vc[vc.size()/2]+vc[vc.size()/2-1]);
}
}
};
原创文章,作者:kirin,如若转载,请注明出处:https://blog.ytso.com/273312.html