Why my answer is runtime error?


  • 0
    2

    class Solution {
    public:
    vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
    sort(nums1.begin(),nums1.end());
    sort(nums2.begin(),nums2.end());
    vector<int> rvec;
    vector<int>::iterator vit1 = nums1.begin();
    vector<int>::iterator vit2 = nums2.begin();

        while(vit1 != nums1.end()&&vit2 != nums2.end())
        {
            while(*vit1 > *vit2)
            {
                vit2++;
            }
            while(*vit1 < *vit2)
            {
                vit1++;
            }
            while(*vit1 == *vit2)
            {
                rvec.push_back(*vit1);
                vit1++;
                vit2++;
            }
        }
    return rvec;
    }
    

    };


  • 0
    D

    The inside while loops are not checking for end of array.

    in the first inner loop, vit2 is incremented without checking for end.


Log in to reply
 

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