添加0027.移除元素 Swift 版本

This commit is contained in:
余杜林
2021-08-07 14:42:17 +08:00
parent 18bf14707c
commit a0fa0ebde6
2 changed files with 19 additions and 0 deletions

BIN
pics/.DS_Store vendored Normal file

Binary file not shown.

View File

@ -216,6 +216,25 @@ fn main() {
println!("{:?}",remove_element(&mut nums, 5));
}
```
Swift:
```swift
func removeElement(_ nums: inout [Int], _ val: Int) -> Int {
var slowIndex = 0
for fastIndex in 0..<nums.count {
if val != nums[fastIndex] {
if slowIndex != fastIndex {
nums[slowIndex] = nums[fastIndex]
}
slowIndex += 1
}
}
return slowIndex
}
```
-----------------------
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
* B站视频[代码随想录](https://space.bilibili.com/525438321)