My C++ solution which is easy to understand.


  • 0
    W
    int removeElement(vector<int>& nums, int val) {
            int len = 0;
            for (int i = 0; i < nums.size(); ++i) {
                if (nums[i] != val) {
                    nums[len] = nums[i];
                    ++len;
                }
            }
            return len;
        }
    

Log in to reply
 

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