Skip to content

Commit 2b1df1e

Browse files
committed
leetcode:27
1 parent 8452539 commit 2b1df1e

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

leetCode/remove_element.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""problem
2+
https://leetcode.com/problems/remove-element/
3+
4+
Given an array nums and a value val, remove all instances of that value in-place and return the new length.
5+
6+
Do not allocate extra space for another array, you must do this by modifying the input array in-place
7+
with O(1) extra memory.
8+
9+
The order of elements can be changed. It doesn't matter what you leave beyond the new length
10+
11+
"""
12+
13+
14+
class Solution:
15+
def removeElement(self, nums, val):
16+
for i in range(len(nums) - 1, -1, -1):
17+
if nums[i] == val:
18+
del nums[i]
19+
return len(nums)
20+
21+
22+
if __name__ == "__main__":
23+
obj = Solution()
24+
nums = [0, 1, 2, 2, 3, 0, 4, 2]
25+
val = 2
26+
print(obj.removeElement(nums, val))

0 commit comments

Comments
 (0)