C++ Simple 3-Lines Using Two Pointers, O(N) Time O(1) Space


  • 0
    M
    class Solution {
    public:
        int removeElement(vector<int>& nums, int val) {
            int cnt = 0;
            for (int n: nums) if (n != val) nums[cnt++] = n;
            return cnt;
        }
    };
    

Log in to reply
 

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