题目:
Given an array and a value, remove all instances of that value in place and return the new length.
Do not allocate extra space for another array, you must do this in place with constant memory.
代码:
int removeElement(vector<int>& nums, int val) {
int n=nums.size();
int res=0;
for(int i=0;i<n;i++){
if(nums[i]!=val)
nums[res++]=nums[i]; //后面非删除元素前移
}
return res; //返回个数
}