LeetCode 4. Median of Two Sorted Arrays
水题
class Solution
{
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2)
{
vector<int> nums3;
int bg1 = 0, bg2 = 0;
while(bg1 < nums1.size() && bg2 < nums2.size())
{
if(nums1[bg1] >= nums2[bg2])
nums3.push_back(nums2[bg2 ++]);
else
nums3.push_back(nums1[bg1 ++]);
}
while(bg1 < nums1.size())
nums3.push_back(nums1[bg1 ++]);
while(bg2 < nums2.size())
nums3.push_back(nums2[bg2 ++]);
int bg3 = nums3.size();
if(bg3 & 1)
return 1.0*nums3[bg3 / 2];
else
return 1.0*(nums3[bg3 / 2] + nums3[bg3 / 2 - 1]) / 2;
}
};