Given an integer array nums and an integer val, remove all occurrences of val in nums in-place. The order of the elements may be changed. Then return the number of elements in nums which are not equal to val.
Consider the number of elements in nums which are not equal to val be k, to get accepted, you need to do the following things:
- Change the array
numssuch that the firstkelements ofnumscontain the elements which are not equal toval. The remaining elements ofnumsare not important as well as the size ofnums. - Return
k.
k
i
0
0
1
1
2
2
2
3
3
4
0
5
4
6
2
7
val=2
Step 1 / 7
Step 1:
Maintain k = next position to write a kept value (invariant: nums[0..k) contains no val).
Pointers: i=0, k=0
Focus: select @ [0]
val=2