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;
    }
};
posted @ 2017-03-04 11:36  aiterator  阅读(102)  评论(0编辑  收藏  举报