mirror of
https://github.com/devenperez/leetcode.git
synced 2026-06-13 14:57:08 +00:00
Time: 37 ms (68.92%), Space: 16.4 MB (9.76%) - LeetHub
This commit is contained in:
@@ -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
|
||||
Reference in New Issue
Block a user