Ruka
1 min readJul 28, 2020

[LeetCode][python3]0027. Remove Element

Start the journey
N2I -2020.03.23

  1. 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)
N2I -2020.03.23

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.”

Ruka
Ruka

Written by Ruka

HI I’m Ruka. Now a SWE in Taiwan. Contact via email: nayzi9999@gmail.com

No responses yet