From 3de2992235e43b6ce40a35624a42a08b5a5a866b Mon Sep 17 00:00:00 2001 From: xuxiaomeng <961592690@qq.com> Date: Sat, 21 May 2022 21:02:59 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A00027.=E7=A7=BB=E9=99=A4?= =?UTF-8?q?=E5=85=83=E7=B4=A0=20Kotlin=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0027.移除元素.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/problems/0027.移除元素.md b/problems/0027.移除元素.md index 590cf0b9..f0cb5375 100644 --- a/problems/0027.移除元素.md +++ b/problems/0027.移除元素.md @@ -329,5 +329,16 @@ int removeElement(int* nums, int numsSize, int val){ } ``` +Kotlin: +```kotlin +fun removeElement(nums: IntArray, `val`: Int): Int { + var slowIndex = 0 // 初始化慢指针 + for (fastIndex in nums.indices) { + if (nums[fastIndex] != `val`) nums[slowIndex++] = nums[fastIndex] // 在慢指针所在位置存储未被删除的元素 + } + return slowIndex + } +``` + -----------------------