mirror of
https://github.com/devenperez/leetcode.git
synced 2026-06-13 23:07:08 +00:00
22 lines
697 B
Python
22 lines
697 B
Python
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 |