标签:
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.
这是一道很简单的题目,对数组进行一次遍历,删除与给定值相等的关键字,返回新长度即可。
class Solution {
public:
int removeElement(vector<int>& nums, int val) {
int i = 0 , k = 0;
while (i < nums.size())
{
if (nums[i] != val)
nums[k++] = nums[i];
i++;
}
return k;
}
};
标签:
原文地址:http://blog.csdn.net/fly_yr/article/details/47447271