2021-04-19 27. 移除元素
思路:双指针
class Solution {
public int removeElement(int[] nums, int val) {
int crt = 0, pre = 0; // [0,pre[是已经处理好的要返回的数组,crt每次如果等于val就往后走直到第一个不是val的数
while(crt<nums.length){
while(crt<nums.length && nums[crt]==val)
crt++;
if(crt!=nums.length){
nums[pre]=nums[crt];
pre++;
crt++;
}
}
return pre;
}
}