-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Expand file tree
/
Copy path0027-remove-element.py
More file actions
25 lines (22 loc) · 852 Bytes
/
0027-remove-element.py
File metadata and controls
25 lines (22 loc) · 852 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
class Solution:
def removeElement(self, nums: List[int], val: int) -> int:
k = 0
for i in range(len(nums)):
if nums[i] != val:
nums[k] = nums[i]
k += 1
return k
# Optimized solution with the same time and space complexity
class Solution:
def removeElement(self, nums: List[int], val: int) -> int:
# Avoid unessary copy operations in a previous solution, when k == i and nums[i] != val
# by swapping nums[i] and the last element of the array (nums[n])
n = len(nums)
i = 0
while i < n:
if nums[i] == val:
nums[i], nums[n - 1] = nums[n - 1], nums[i]
n -= 1 # decrement the length of the array by discarding the last element
else:
i += 1
return n