Timeline
Timeline
2025-09-29
init
Two Pointers
Title:
This simple and direct method can still pass, but it is very inefficient, almost barely passing.
1 |
|
Using the two-pointer method (fast and slow pointers) is more efficient, ‘overwriting’ the extra elements on the original array.
- Slow pointer (slow): points to the end of the current result array.
- Fast pointer (fast): traverses the entire array.
- If slow < 2, keep directly (because the first two elements must be kept regardless).
- If nums[fast] != nums[slow - 2], it means the new number has not appeared more than twice, overwrite the value of nums[slow].
- Otherwise skip.
1 | class Solution { |
