From bdc06a4960a6ce17c4a279e9f0b8220a2a2b059f Mon Sep 17 00:00:00 2001 From: 502y <53784463+502y@users.noreply.github.com> Date: Sat, 13 Apr 2024 09:51:06 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200027.=E7=A7=BB=E9=99=A4?= =?UTF-8?q?=E5=85=83=E7=B4=A0.md=20Dart=E7=9B=B8=E5=90=91=E5=8F=8C?= =?UTF-8?q?=E6=8C=87=E9=92=88=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0027.移除元素.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/problems/0027.移除元素.md b/problems/0027.移除元素.md index cb342586..5f681c5f 100644 --- a/problems/0027.移除元素.md +++ b/problems/0027.移除元素.md @@ -476,6 +476,32 @@ public class Solution { } ``` +###Dart: +```dart +int removeElement(List nums, int val) { + //相向双指针法 + var left = 0; + var right = nums.length - 1; + while (left <= right) { + //寻找左侧的val,将其被右侧非val覆盖 + if (nums[left] == val) { + while (nums[right] == val&&left<=right) { + right--; + if (right < 0) { + return 0; + } + } + nums[left] = nums[right--]; + } else { + left++; + } + } + //覆盖后可以将0至left部分视为所需部分 + return left; +} + +``` +