My 3ms solution in C++


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

Log in to reply
 

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