Conceptually very clean solution C++


  • -1
    B
    double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
        nums1.insert(nums1.begin(), INT_MIN); nums1.push_back(INT_MAX);
        nums2.insert(nums2.begin(), INT_MIN); nums2.push_back(INT_MAX);
        int b1 = 0, b2 = 0, e1 = nums1.size() - 1, e2 = nums2.size() - 1;
        
        do {
            int bm = min(nums1[b1], nums2[b2]);
            int em = max(nums1[e1], nums2[e2]);
            if (em <= bm) return (double)(em + bm)/(double)2;
            else {
                (*((bm == nums1[b1]) ? &b1 : &b2))++;
                (*((em == nums1[e1]) ? &e1 : &e2))--;
            }
        } while (true);
        return (double)0;
    }

Log in to reply
 

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