Skip to content

Commit e106d40

Browse files
committed
add 0027_remove_element.py
1 parent e8451cc commit e106d40

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

problems/easy/0027_remove_element.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
"""
2+
Given an integer array nums and an integer val, remove all occurrences
3+
of val in nums in-place. The relative order of the elements may be changed.
4+
5+
Since it is impossible to change the length of the array in some languages,
6+
you must instead have the result be placed in the first part of the array
7+
nums. More formally, if there are k elements after removing the duplicates,
8+
then the first k elements of nums should hold the final result. It does not
9+
matter what you leave beyond the first k elements.
10+
11+
Return k after placing the final result in the first k slots of nums.
12+
13+
Do not allocate extra space for another array. You must do this by modifying
14+
the input array in-place with O(1) extra memory.
15+
16+
Example 1:
17+
Input: nums = [3,2,2,3], val = 3
18+
Output: 2, nums = [2,2,_,_]
19+
Explanation: Your function should return k = 2, with the first two elements
20+
of nums being 2. It does not matter what you leave beyond the returned k
21+
(hence they are underscores).
22+
23+
Example 2:
24+
Input: nums = [0,1,2,2,3,0,4,2], val = 2
25+
Output: 5, nums = [0,1,4,0,3,_,_,_]
26+
Explanation: Your function should return k = 5, with the first five elements
27+
of nums containing 0, 0, 1, 3, and 4. Note that the five elements can be
28+
returned in any order. It does not matter what you leave beyond the returned
29+
k (hence they are underscores).
30+
31+
Constraints:
32+
* 0 <= nums.length <= 100
33+
* 0 <= nums[i] <= 50
34+
* 0 <= val <= 100
35+
"""
36+
37+
class Solution:
38+
# O(n) approach using two pointers (i - front, current value;
39+
# j - back, used to swap with i if i is matching val)
40+
def removeElement(self, nums: List[int], val: int) -> int:
41+
i = 0
42+
j = len(nums) - 1
43+
44+
while i <= j:
45+
if nums[i] == val:
46+
if nums[j] != val:
47+
nums[j], nums[i] = nums[i], nums[j]
48+
i += 1
49+
j -= 1
50+
else:
51+
i += 1
52+
53+
return max(j+1, 0)
54+
55+
# O(n) approach using two pointers going in the same direction
56+
# (i - current value; j - used to swap with i if i is matching val)
57+
#def removeElement(self, nums: List[int], val: int) -> int:
58+
# j = 0
59+
# k = len(nums)
60+
# for i in range(len(nums)):
61+
# if nums[i] == val:
62+
# if j == 0:
63+
# j = i + 1
64+
65+
# while j < len(nums) and nums[j] == val:
66+
# j += 1
67+
68+
# if j > len(nums) - 1:
69+
# k = i
70+
# break
71+
72+
# nums[i], nums[j] = nums[j], nums[i]
73+
# j += 1
74+
75+
# return k
76+
77+
# O(n) approach iterating through all elements of nums
78+
#def removeElement(self, nums: List[int], val: int) -> int:
79+
# prev = 0
80+
# for i in range(len(nums)):
81+
# if nums[i] != val:
82+
# nums[prev] = nums[i]
83+
# prev += 1
84+
85+
# return prev

0 commit comments

Comments
 (0)