解法:双指针
相似题:283.移动零
这道题时283题的升级版,从移动零变成移动任意数。
int removeElement(int* nums, int numsSize, int val){
int left = 0, right = 0;
while(right < numsSize)
{
if(nums[right] != val)
{
nums[left++] = nums[right++];
}
else right++;
}
return left;
}
时间复杂度 O ( n ) O(n) O(n),空间复杂度 O ( 1 ) O(1) O(1)。