1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
| // @Title: 加一 (Plus One)
// @Author: 15816537946@163.com
// @Date: 2021-10-21 13:53:44
// @Runtime: 0 ms
// @Memory: 2 MB
func plusOne(digits []int) []int {
// 每次只能加1位
var forwordCnt int
n := len(digits)
for i := n - 1; i >= 0; i-- {
if digits[i]+1 < 10 {
digits[i] += 1
return digits
} else {
digits[i] = 0
forwordCnt++
}
}
rebuild := make([]int, forwordCnt+1)
rebuild[0] = 1
for i := 1; i < forwordCnt; i++ {
rebuild[i] = 0
}
return rebuild
}
|