题目:
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.
Solution:
思路跟上一题类似。
int removeElement(vector<int>& nums, int val)
{
int n =nums.size();
if(n == 0)
return n;
vector<int>::iterator it;
int count = 0;
it = nums.begin();
while(it != nums.end())
{
if(*it == val)
{
it = nums.erase(it);
}
else
{
count++;
it++;
}
}
/*for(it = nums.begin();it!=nums.end();it++)
cout<<*it<<" ";
cout<<endl;*/
return count;
}