Add solution for 0027 leetcode task
This commit is contained in:
parent
1c5b792565
commit
04948814fe
2 changed files with 24 additions and 0 deletions
|
@ -1,3 +1,4 @@
|
|||
# Link to task: https://leetcode.com/problems/remove-duplicates-from-sorted-array/
|
||||
from typing import List
|
||||
|
||||
class Solution:
|
||||
|
|
23
leetcode/static_arrays/0027_remove_element.py
Normal file
23
leetcode/static_arrays/0027_remove_element.py
Normal file
|
@ -0,0 +1,23 @@
|
|||
# Link: https://leetcode.com/problems/remove-element/description/
|
||||
from typing import List
|
||||
|
||||
|
||||
class Solution:
|
||||
def removeElement(self, nums: List[int], val: int) -> int:
|
||||
c = 0
|
||||
n = len(nums) - 1
|
||||
while n >= 0:
|
||||
if nums[n] == val:
|
||||
del nums[n]
|
||||
else:
|
||||
c += 1
|
||||
n -= 1
|
||||
return c
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# nums = [0, 1, 2, 2, 3, 0, 4, 2]
|
||||
nums = [3, 2, 2, 3]
|
||||
print(nums)
|
||||
print(Solution().removeElement(nums, 3))
|
||||
print(nums)
|
Loading…
Reference in a new issue