原题链接:https://leetcode.com/problems/remove-element/
题意:删除数组中与val相同的元素,并返回该数组的长度;并更新被删除后的数组。
class Solution {
public:
int removeElement(vector<int>& nums, int val) {
int index = 0;
int n = nums.size();
for (int i = 0; i < n; ++i) {
if (nums[i] != val) {
nums[index++] = nums[i];
}
}
return index;
}
};