mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-05 08:27:30 +08:00
Add solution 989
This commit is contained in:
@ -0,0 +1,25 @@
|
||||
package leetcode
|
||||
|
||||
func addToArrayForm(A []int, K int) []int {
|
||||
res := []int{}
|
||||
for i := len(A) - 1; i >= 0; i-- {
|
||||
sum := A[i] + K%10
|
||||
K /= 10
|
||||
if sum >= 10 {
|
||||
K++
|
||||
sum -= 10
|
||||
}
|
||||
res = append(res, sum)
|
||||
}
|
||||
for ; K > 0; K /= 10 {
|
||||
res = append(res, K%10)
|
||||
}
|
||||
reverse(res)
|
||||
return res
|
||||
}
|
||||
|
||||
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]
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user