Remove element

Tag: array, level : easy

Remove element_第1张图片

Solution:

maintain two pointers, one iterate the whole array, one keep the tail valid element position.

when the iterated pointer meet a valid element, assign it the the tail position, and make the tail pointer one step further.

When the iteration done, the tail position is the result.

Time: O(n), Space: O(1)


Remove element_第2张图片

你可能感兴趣的:(Remove element)