SOS... What's wrong with my code T_T


  • 0
    E
    class Solution {
    public:
        int removeElement(vector<int>& nums, int val) {
            int cnt = 0;
            for(int i = 0; i < nums.size() - cnt; ++i){
                if(nums[i] == val){
                    ++cnt;
                    swap(nums[i], nums[nums.size() - cnt]);
                    --i;
                }
            }
            return cnt;
        }
    };
    
    Input:
    [1]
    1
    Output:
    [1]
    Expected:
    []
    

Log in to reply
 

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