Skip to content

Commit 9c6ca65

Browse files
committed
feat: remove-element
1 parent 30a471a commit 9c6ca65

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

Diff for: pointer.remove-element.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
"""
6+
https://leetcode-cn.com/problems/remove-element
7+
27. 移除元素
8+
给你一个数组 nums 和一个值 val,你需要 原地 移除所有数值等于 val 的元素,并返回移除后数组的新长度。
9+
不要使用额外的数组空间,你必须仅使用 O(1) 额外空间并 原地 修改输入数组。
10+
元素的顺序可以改变。你不需要考虑数组中超出新长度后面的元素。
11+
"""
12+
def removeElement(self, nums: List[int], val: int) -> int:
13+
a = 0
14+
b = 0
15+
16+
while a < len(nums):
17+
if nums[a] != val:
18+
nums[b] = nums[a]
19+
b += 1
20+
a += 1
21+
22+
return b
23+
24+
25+
so = Solution()
26+
print(so.removeElement([0,1,2,2,3,0,4,2], 2))

0 commit comments

Comments
 (0)