为数组篇 增加 Swift 实现

This commit is contained in:
zhenzi
2021-07-17 12:13:15 +08:00
parent 2c4afffbd3
commit c14c2297f1
14 changed files with 702 additions and 1 deletions

View File

@@ -85,3 +85,23 @@ class Solution:
arr[0] = 1
return arr
```
Swift Code:
```swift
class Solution {
func plusOne(_ digits: [Int]) -> [Int] {
let count = digits.count
var digits = digits
for i in stride(from: count - 1, through: 0, by: -1) {
digits[i] = (digits[i] + 1) % 10
if digits[i] != 0 {
return digits
}
}
var arr: [Int] = Array.init(repeating: 0, count: count + 1)
arr[0] = 1
return arr
}
}
```