[LeetCode][python3]0027. Remove Element
Start the journey
N2I -2020.03.23
- My first solution
class Solution:
def removeElement(self, nums: List[int], val: int) -> int:
for i in reversed(range(len(nums))):
if nums[i]==val:
del nums[i]
return len(nums)
Note:
Explanation:
Delete the element in an array reversely because it will change the index after the element you delete.
Notice that we have to follow the condition in the question: “Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.”