Error when I use function erase(),how to fix that?


  • 0
    L
    int removeElement(vector<int>& nums, int val) {
    		int lgt=0;
    		for (int i = 0; i < nums.size(); i++)
    		{
    			if (nums[i]==val)
    			{
    				lgt++;
    				nums.erase(nums.begin()+i--);
    			}
    		}
    		return nums.size()-lgt;
    }

  • 0
    L
       Submission Result: Wrong Answer
        
        Input: [1], 1
        Output: ]
        Expected: []

  • 0
    L

    output ']'
    What does it mean?


  • 0
    P

    instead returning "num.size() - lgt" you should return "len - lgt" where len is the initial size of vector.
    as you are returning num.size() - lgt it is wrong .


Log in to reply
 

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