Time: 37 ms (68.92%), Space: 16.4 MB (9.76%) - LeetHub

This commit is contained in:
Deven
2023-05-21 22:17:24 -04:00
parent c82046d95e
commit cd75ebecc3
+22
View File
@@ -0,0 +1,22 @@
class Solution:
def removeElement(self, nums: List[int], val: int) -> int:
if len(nums) == 0 :
return 0
backIndex = len(nums) - 1
i = 0
while i < backIndex:
if nums[i] == val :
while nums[backIndex] == val:
backIndex -= 1
if backIndex == i:
return backIndex if nums[backIndex] == val else backIndex + 1
nums[i] = nums[backIndex]
nums[backIndex] = val
backIndex -= 1
i += 1
return backIndex if nums[backIndex] == val else backIndex + 1