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 + } +``` + -----------------------