27. Remove Element

problem

solution

1
2
3
4
5
6
7
8
9
10
11
class Solution {
public:
int removeElement(vector<int>& nums, int val) {
int slow = 0, fast = 0, n = nums.size();
while(fast<n){
if(nums[fast] !=val ) nums[slow++] = nums[fast];
fast++;
}
return slow;
}
};

analysis

  • time complexity O(n)
  • space complexity O(1)