Why am I getting run time error?


  • 0
    Y
    class Solution{
    public:
        bool containsDuplicate(vector<int> &nums){
            sort(nums.begin(),nums.end());
            for (int i = 0; i < nums.size()-1; i++){
                if (nums[i] == nums[i+1]) {
                    return true;
                }
            }
            return false;
        }
    };
    

    This is my code and when it run on the empty vector test case it give me run time error. Can anyone tell what the problem is?


  • 0
    A

    You should judge whether the nums is empty. Write down this sentence in the first row:
    if (nums.size() == 0)
    return false;


Log in to reply
 

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