Different result when I run the code vs submitted code.


  • 0
    H

    For this problem- I get correct answer when I run the code & incorrect answer upon submission. I am not sure why it works that way.
    The input is [1,1], [1].

    Code below:

        vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
            sort(nums1.begin(), nums1.end());
            sort(nums2.begin(), nums2.end());
            int i, j;
            vector<int> res;
            res.clear(); 
            while(i < nums1.size() && j < nums2.size())
            {
                if(nums1[i] == nums2[j])
                {
                    res.push_back(nums1[i]);
                    i ++;
                    j ++;
                }
                else if(nums1[i] < nums2[j])
                {
                    i ++;
                }
                else
                    j ++;
            }
            return res;
        }
    

Log in to reply
 

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