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: 数组形式的整数加法 (Add to Array-Form of Integer)
// @Author: 15816537946@163.com
// @Date: 2021-01-22 23:57:57
// @Runtime: 36 ms
// @Memory: 6.8 MB
func addToArrayForm(A []int, K int) (ans []int) {
	for i := len(A) - 1; i >= 0; i-- {
		sum := A[i] + K%10
		K /= 10
		if sum >= 10 {
			K++
			sum -= 10
		}
		ans = append(ans, sum)
	}
	for ; K > 0; K /= 10 {
		ans = append(ans, K%10)
	}
	reverse(ans)
	return
}

func reverse(A []int) {
	for i, n := 0, len(A); i < n/2; i++ {
		A[i], A[n-1-i] = A[n-1-i], A[i]
	}
}