My 5ms c++ solution


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

Log in to reply
 

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