-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathremove-element.py
More file actions
44 lines (33 loc) · 869 Bytes
/
Copy pathremove-element.py
File metadata and controls
44 lines (33 loc) · 869 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
class Solution:
def removeElement(self, nums, val):
"""
:type nums: List[int]
:type val: int
:rtype: int
"""
pos = 0
shift = 0
nums_length = len(nums)
while pos < nums_length:
spos = pos - shift
if nums[spos] == val:
del nums[spos]
shift += 1
pos += 1
return nums_length - shift
solution = Solution()
arr = []
assert solution.removeElement(arr, 123) == 0
assert arr == []
arr = [123]
assert solution.removeElement(arr, 123) == 0
assert arr == []
arr = [1]
assert solution.removeElement(arr, 123) == 1
assert arr == [1]
arr = [3, 2, 2, 3]
assert solution.removeElement(arr, 3) == 2
assert arr == [2, 2]
arr = [0, 1, 2, 2, 3, 0, 4, 2]
assert solution.removeElement(arr, 2) == 5
assert arr == [0, 1, 3, 0, 4]