题目描述:
Given an array nums and a value val, remove all instances of that value in-placeand return the new length.
Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
The order of elements can be changed. It doesn‘t matter what you leave beyond the new length.
思路:
定义区间 [0,k)范围为无val的数列,遍历nums
代码如下:
class Solution { public: int removeElement(vector<int>& nums, int val) { if(nums.size() == 0) return 0; int k = 0; for(int i = 0; i < nums.size(); ++i) { if(nums[i] != val) nums[k++] = nums[i]; } return k; } };
原文:https://www.cnblogs.com/harchar/p/10502029.html