您的位置:首页 > 其它

leetcode——4——Median of Two Sorted Arrays

2016-04-08 12:00 253 查看
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) {

int m = nums1.size();
int n = nums2.size();

vector<int> sum;
int i=0;
int j=0;
while(i<m&&j<n)
{
if(nums1[i]>nums2[j])
{
sum.push_back(nums2[j]);
j++;
}
else
{
sum.push_back(nums1[i]);
i++;
}

}
while(i<m)
{
sum.push_back(nums1[i]);
i++;
}
while(j<n)
{
sum.push_back(nums2[j]);
j++;
}
return ((double)(sum[(m+n-1)/2]+sum[(m+n)/2]))/2;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: