Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Commit eb59b9c

Browse files
committed
989 accepted. 216ms
1 parent 656b8a8 commit eb59b9c

File tree

1 file changed

+20
-19
lines changed

1 file changed

+20
-19
lines changed
Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
11
package problem0989
22

3-
import "strconv"
4-
53
func addToArrayForm(A []int, K int) []int {
6-
a := slice2num(A)
7-
a += K
8-
return num2Slice(a)
9-
}
10-
11-
func slice2num(A []int) int {
12-
res := 0
13-
for _, v := range A {
14-
res = res*10 + v
4+
reverse(A)
5+
A[0] += K
6+
size := len(A)
7+
for i := 0; i+1 < size && A[i] > 9; i++ {
8+
A[i+1] += A[i] / 10
9+
A[i] %= 10
10+
}
11+
var tail int
12+
for A[size-1] > 9 {
13+
A[size-1], tail = A[size-1]%10, A[size-1]/10
14+
A = append(A, tail)
15+
size++
1516
}
16-
return res
17+
reverse(A)
18+
return A
1719
}
1820

19-
func num2Slice(n int) []int {
20-
s := strconv.Itoa(n)
21-
bytes := []byte(s)
22-
res := make([]int, len(bytes))
23-
for i, b := range bytes {
24-
res[i] = int(b - '0')
21+
func reverse(A []int) {
22+
i, j := 0, len(A)-1
23+
for i < j {
24+
A[i], A[j] = A[j], A[i]
25+
i++
26+
j--
2527
}
26-
return res
2728
}

0 commit comments

Comments
 (0)