Remove element
The question requires the
removal of all the elements val in the array, the time complexity is O(N), the space complexity is O(1), and the length of the remaining elements is returned.
The idea
problem requires the space complexity to be O(1), so the space cannot be opened up, and only operations can be performed on the original array. The time complexity is O(N). We can use the method of covering to cover the original array.
Code
int removeElement(int* nums, int numsSize, int val){
int p1 = 0;
for (int i = 0; i < numsSize; i++)
{
if (nums[i] != val)
{
nums[p1] = nums[i];
p1++;
}
}
return p1;
}