11 lines, 4ms, c++ solution share


  • 0
    T
    int removeElement(vector<int>& nums, int val) {
        int pos = 0, end = nums.size()-1;
        while(end>=pos && nums[end]==val) end--;
        for(;pos<nums.size() && end>= pos; pos++){
            if(nums[pos] == val){
                swap(nums[pos], nums[end]);
                end--;
                while(end>=pos && nums[end]==val) end--;
            }
        }
        return pos;
    }

Log in to reply
 

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