您的位置:首页 > 其它

leetcode Median of Two Sorted Arrays

2015-05-21 00:53 375 查看
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)).

class Solution {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
nums1.insert(nums1.begin(),nums2.begin(),nums2.end());
sort(nums1.begin(),nums1.end());

int len = nums1.size();
if (len % 2 == 0)
{
return (nums1[len/2] + nums1[len/2-1])/(double)2;
}

return nums1[len/2];
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: