From 424d5c224f229aaee353b3bbae8996b8e5dcbef2 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Wed, 3 May 2023 15:06:11 -0500 Subject: [PATCH] =?UTF-8?q?Update=200027.=E7=A7=BB=E9=99=A4=E5=85=83?= =?UTF-8?q?=E7=B4=A0.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0027.移除元素.md | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/problems/0027.移除元素.md b/problems/0027.移除元素.md index 91150c74..6dc6404e 100644 --- a/problems/0027.移除元素.md +++ b/problems/0027.移除元素.md @@ -198,6 +198,7 @@ Python: ``` python 3 +(版本一)快慢指针法 class Solution: def removeElement(self, nums: List[int], val: int) -> int: # 快慢指针 @@ -213,7 +214,21 @@ class Solution: return slow ``` - +``` python 3 +(版本二)暴力递归法 +class Solution: + def removeElement(self, nums: List[int], val: int) -> int: + i, l = 0, len(nums) + while i < l: + if nums[i] == val: # 找到等于目标值的节点 + for j in range(i+1, l): # 移除该元素,并将后面元素向前平移 + nums[j - 1] = nums[j] + l -= 1 + i -= 1 + i += 1 + return l + +``` Go: