Why my C++ code not accepted while run correctly on my machine


  • 0
    L
    class Solution {
    public:
        int removeElement(vector<int>& nums, int val) {
            vector<int>::iterator iter = find(nums.begin(), nums.end(), val);
            int count = 0;
            while(iter != nums.end()) {
                nums.erase(iter);
                iter = find(nums.begin(), nums.end(), val);
                count ++;
            }
            return count;
        }
    };
    
    the input and output like this:
    Input:
    [1], 1
    Output:
    [1]
    Expected:
    []

  • 0

    Well you clearly return "count" 1 instead of the correct 0, as you're counting the deletions instead of the remaining elements.


  • 0
    L

    if input [1],1, my program return [],0, that is right, but the judge is wrong


  • 0

    No, you don't return [], 0. As I already said, you clearly return 1.


  • 0
    L

    oh, you are right. i should return nums.size()


Log in to reply
 

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