My C++ solution beats 99.28% solutions, easy to understand


  • 0
    S

    '''
    double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
    int i=0,j=0;
    vector<int> ans;
    while(i < nums1.size() && j < nums2.size()){
    if(nums1[i]>=nums2[j])
    ans.push_back(nums2[j++]);
    else
    ans.push_back(nums1[i++]);
    }
    while(i < nums1.size()) ans.push_back(nums1[i++]);
    while(j < nums2.size()) ans.push_back(nums2[j++]);
    int k = ans.size();
    return ((k%2)==0) ? (double)(ans[k/2] + ans[(k/2)-1])/2 : (double)ans[k/2];
    }
    '''


Log in to reply
 

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