From f49b2e4a75c6f98c082923e1098aedc5cb27574b Mon Sep 17 00:00:00 2001 From: JaneyLin <105125897+janeyziqinglin@users.noreply.github.com> Date: Mon, 6 Jun 2022 21:49:32 -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 the former code has not consider if nums is None or len(nums)==0 --- problems/0027.移除元素.md | 38 ++++++++++++++++------------------- 1 file changed, 17 insertions(+), 21 deletions(-) diff --git a/problems/0027.移除元素.md b/problems/0027.移除元素.md index 4b50d666..b239136c 100644 --- a/problems/0027.移除元素.md +++ b/problems/0027.移除元素.md @@ -173,28 +173,24 @@ class Solution { Python: -```python +```python3 class Solution: - """双指针法 - 时间复杂度:O(n) - 空间复杂度:O(1) - """ - - @classmethod - def removeElement(cls, nums: List[int], val: int) -> int: - fast = slow = 0 - - while fast < len(nums): - - if nums[fast] != val: - nums[slow] = nums[fast] - slow += 1 - - # 当 fast 指针遇到要删除的元素时停止赋值 - # slow 指针停止移动, fast 指针继续前进 - fast += 1 - - return slow + def removeElement(self, nums: List[int], val: int) -> int: + if nums is None or len(nums)==0: + return 0 + l=0 + r=len(nums)-1 + while l