From e894f44b607b0a3433cae8284a9e2c0c85f86645 Mon Sep 17 00:00:00 2001 From: Steve <841532108@qq.com> Date: Fri, 31 Dec 2021 19:23:47 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880027.=E7=A7=BB?= =?UTF-8?q?=E9=99=A4=E5=85=83=E7=B4=A0.md=EF=BC=89=EF=BC=9A=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=20typescript=20=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0027.移除元素.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/problems/0027.移除元素.md b/problems/0027.移除元素.md index 693684f3..99990302 100644 --- a/problems/0027.移除元素.md +++ b/problems/0027.移除元素.md @@ -197,7 +197,23 @@ var removeElement = (nums, val) => { }; ``` +TypeScript: + +```typescript +function removeElement(nums: number[], val: number): number { + let slowIndex: number = 0, fastIndex: number = 0; + while (fastIndex < nums.length) { + if (nums[fastIndex] !== val) { + nums[slowIndex++] = nums[fastIndex]; + } + fastIndex++; + } + return slowIndex; +}; +``` + Ruby: + ```ruby def remove_element(nums, val) i = 0