题目大意
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.
要求从数组中删除指定值的元素,并返回新数组的长度。
C++解决方案:
class Solution {
public:
int removeElement(vector<int>& nums, int val) {
int begin = 0;
int n = nums.size();
for(int i = 0; i < n; i++)
if(nums[i] != val)
nums[begin++] = nums[i];
return begin;
}
};