Why my code output 0.000 on Leetcode, but correct on my own computer?


  • 0
    1

    input: {2,2,2,2}, {2,2,2}

    class Solution {
    public:

    double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
    int m = nums1.size(), n = nums2.size();
    if((m+n) % 2 == 1) {
            return (double)findKth(nums1, 0, nums2, 0, (m+n)/2 + 1);
        } else {
            int a = findKth(nums1, 0, nums2, 0, (m+n)/2);
            int b = findKth(nums1, 0, nums2, 0, (m+n)/2 + 1);
            return (double)(a+b) / 2.0;
        }
    }
    
    int findKth(vector<int> &nums1, int start1, vector<int> &nums2, int start2, int k) {
    if(start1 >= nums1.size())
        return nums2[start2 + k - 1];
    if(start2 >= nums2.size())
        return nums1[start1 + k - 1];
    
    if(k == 1)
        return min(nums2[start1], nums2[start2]);
    
    int aKey = start1 + k/2 - 1 < nums1.size()? nums1[start1 + k/2 -1] : INT_MAX;
    int bKey = start2 + k/2 - 1 < nums2.size()? nums2[start2 + k/2 -1] : INT_MAX;
    if(aKey > bKey)
        return findKth(nums1, start1, nums2, start2 + k/2, k - k/2);
    else
        return findKth(nums1, start1 + k/2, nums2, start2, k - k/2);
    }
    

    };


  • 0
        return min(nums2[start1], nums2[start2]);
    

    First nums2 should be nums1?


  • 0
    1

    oh...Thank you!


Log in to reply
 

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