mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-11 04:54:51 +08:00
添加0027.移除元素 Swift 版本
This commit is contained in:
BIN
pics/.DS_Store
vendored
Normal file
BIN
pics/.DS_Store
vendored
Normal file
Binary file not shown.
@ -216,6 +216,25 @@ fn main() {
|
|||||||
println!("{:?}",remove_element(&mut nums, 5));
|
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)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||||
|
Reference in New Issue
Block a user