A Simple Constant Space C++ Solution


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

Log in to reply
 

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