-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path27.py
37 lines (25 loc) · 824 Bytes
/
27.py
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
'''27. Remove Element
Created on 2024-11-26 13:15:30
2024-11-26 13:46:36
@author: MilkTea_shih
'''
#%% Packages
#%% Variable
#%% Functions
class Solution:
def removeElement(self, nums: list[int], val: int) -> int:
for index, num in enumerate(nums):
if num == val:
while nums and nums[-1] == val: #element not equal to `val`,
nums.pop() #to find from the bottom
# because of O(1) with list.pop() and O(n) with list.pop(0)
if index >= len(nums):
break
nums[index] = nums.pop() #last and not equal to `val` element
return len(nums)
#%% Main Function
Solution().removeElement([0, 1, 2, 2, 2], 2)
#%% Main
if __name__ == '__main__':
pass
#%%