3ms C++ solution easy to understand


  • 0
    Y

    class Solution {
    public:
    int removeElement(vector<int>& nums, int val) {
    int moveFront=0;
    int end=nums.size();
    for(int i=0;i<end ;i++)
    {
    if(moveFront!=0)
    nums[i-moveFront]=nums[i];

            if(nums[i]==val)
            {
                moveFront++;
            }
        }
        return end -moveFront;
    }
    

    };


Log in to reply
 

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