方法一:双指针(首末版)
class Solution {
public:
int removeElement(vector<int>& nums, int val) {
int size = nums.size();
int end = nums.size();
int start = 0;
int length = 0;
while(start<end)
{
if(nums[start]==val)
{
nums[start]=nums[end-1];
end--;
}
if(nums[start]!=val)
{
start++;
}
}
return start;
}
};
方法二:双指针(开头版)
class Solution {
public:
int removeElement(vector<int>& nums, int val) {
int left = 0, right = nums.size();
while (left < right) {
if (nums[left] == val) {
nums[left] = nums[right - 1];
right--;
} else {
left++;
}
}
return left;
}
};