1. Description
2. Solution
- Version 1
class Solution {
public:
int removeElement(vector<int>& nums, int val) {
int count = 0;
for(int i = 0; i < nums.size(); i ) {
if(nums[i] == val) {
count ;
continue;
}
nums[i - count] = nums[i];
}
return nums.size() - count;
}
};
- Version 2
class Solution {
public:
int removeElement(vector<int>& nums, int val) {
int count = 0;
for(int i = 0; i < nums.size(); i ) {
if(nums[i] != val) {
nums[count] = nums[i];
count ;
}
}
return count;
}
};