mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
添加0027.移除元素 Kotlin版本
This commit is contained in:
@ -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
|
||||
}
|
||||
```
|
||||
|
||||
-----------------------
|
||||
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
||||
|
Reference in New Issue
Block a user