Given an array andavalue, remove all instances of that valuein place andreturnthenewlength.
The order of elements can be changed. It doesn't matter what you leave beyond the new length.
代码
class Solution {
public:
int removeElement(vector<int>& nums, int val) {
if (nums.begin() == nums.end()) return0;
vector<int>::iterator itor;
for (itor = nums.begin(); itor + 1 != nums.end(); )
{
if (*(itor + 1) == val) {
nums.erase(itor + 1);
}
else {
itor++;
}
}
itor = nums.begin();
if (*itor == val) {
nums.erase(itor);
return nums.size();
}
return nums.size();
}
};
翻译这里写代码片原文Given an array and a value, remove all instances of that value in place and return the new length.The order of elements can be changed. It doesn't matter what you leave beyond the new length.