C++ solution:


  • -1
    M
    class Solution {
    public:
        double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) 
        {
            
            unsigned int s1 = nums1.size();
            unsigned int s2 = nums2.size();
            
            int i = 0, j = 0;
            vector<int> v;
            
            while(i < s1 || j < s2)
            {
                if(j == s2)
                    v.push_back(nums1[i++]);
                else if(i == s1)
                    v.push_back(nums2[j++]);
                else
                    (nums1[i]<nums2[j]) ? v.push_back(nums1[i++]) : v.push_back(nums2[j++]);
                
            }
            
            int size = v.size();
            
            return( (size % 2)? (v[size/2]):(((double)v[size/2]+v[size/2-1])/2) );
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.