3ms C++ solution


  • 0
    C
    class Solution {
    public:
        int removeElement(vector<int>& nums, int val) {
            int end = (int)nums.size() - 1;
            int i = 0;
    
            while(i <= end){
                if(nums[i] == val){
                    if(i == end)
                        end--;
                    else{
                        while(nums[end] == val)
                            end--;
                        if(end > i)
                            swap(nums[i], nums[end]);
                    }
                }
                i++;
            }
    
            return end + 1;
        }
    };
    

Log in to reply
 

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