C++ solution


  • 0
    X

    class Solution {
    public:
    int removeElement(vector<int>& nums, int val) {
    int fillIndex = 0;

        while(fillIndex < nums.size() && nums[fillIndex] != val){
            fillIndex++;
        }
        
        if(fillIndex == nums.size()) return fillIndex;
        
        int i = fillIndex;
        
        while(i < nums.size()){
            while(i < nums.size() && nums[i] == val){
                i++;
            }
            if(i != nums.size()){
                nums[fillIndex++] = nums[i++];
            }
        }
        
        return fillIndex;
    }
    

    };


Log in to reply
 

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