天天看點

Leetcode 線性表 Remove Element

本文為senlie原創,轉載請保留此位址:

 Total Accepted: 13840 Total

Submissions: 42676

Given an array and a value, remove all instances of that value in place and return the new length.

The order of elements can be changed. It doesn‘t matter what you leave beyond the new length.

題意:移除數組中出現的給定元素,并傳回移除元素數組長度。要求在數組上操作

思路:周遊數組,将除給定元素外的其他元素指派給“新數組”,不過這個“新數組”還是在原來的“舊數組”的記憶體空間

因為周遊舊數組的下标總大于等于新數組的下标,是以指派并不會影響到還沒有周遊到的元素

複雜度:時間O(n), 空間O(1)