It's so easy, why need any complex explanations?


  • 0
    1

    Extremely Easy to Understand, no explanation needed I think.

    #include<algorithms>
    using namespace std;
    int getNext(vector<int>& nums1, vector<int>& nums2)
    {
        if(nums1.empty()){
            int a = nums2.front();
            nums2.erase(nums2.begin());
            return a;
        }
        
        if(nums2.empty()){
            int a = nums1.front();
            nums1.erase(nums1.begin());
            return a;
        }
        
        int a = nums1.front();
        int b = nums2.front();
        if(a > b){
            nums2.erase(nums2.begin());
            return b;
        }else{
            nums1.erase(nums1.begin());
            return a;
        }
    }
    class Solution {
    public:
        double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
            int m = nums1.size();
            int n = nums2.size();
            double mid = (m + n - 1) / 2.0;
            vector<int> result;
            for(int i = 0; i < mid + 1; i++)
            {
                int num = getNext(nums1, nums2);
                if(abs(i - mid) < 1){
                    result.push_back(num);
                }
            }
            
            double ret = 0;
            for(int i = 0; i < result.size(); i++){
                ret += result[i];
            }
            
            return ret / result.size();
        }
    };
    

Log in to reply
 

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