-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path989.数组形式的整数加法.go
66 lines (62 loc) · 1.12 KB
/
989.数组形式的整数加法.go
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
* @lc app=leetcode.cn id=989 lang=golang
*
* [989] 数组形式的整数加法
*/
// @lc code=start
// func addToArrayForm(A []int, K int) []int {
// var B []int
// var ans []int
// for K > 0 {
// bit := K % 10 // 低位
// K /= 10
// B = append([]int{bit}, B...)
// }
// lenA, lenB := len(A), len(B)
// carry := 0
// for i, j := lenA-1, lenB-1; i > -1 || j > -1; {
// a, b := 0, 0
// if i > -1 {
// a = A[i]
// }
// if j > -1 {
// b = B[j]
// }
// sum := a + b + carry
// carry = sum / 10
// ans = append([]int{sum % 10}, ans...)
// i--
// j--
// }
// if carry > 0 {
// ans = append([]int{carry}, ans...)
// }
// return ans
// }
func addToArrayForm(A []int, K int) []int {
carry := 0
var ans []int
for i := len(A) - 1; ; i-- {
if i < 0 && K == 0 {
break
}
a := 0
if i > -1 {
a = A[i]
}
bit := K % 10
K /= 10
sum := a + bit + carry
carry = sum / 10
ans = append(ans, sum%10)
}
if carry > 0 {
ans = append(ans, carry)
}
// reverse
for i := 0; i < len(ans)/2; i++ {
ans[i], ans[len(ans)-1-i] = ans[len(ans)-1-i], ans[i]
}
return ans
}
// @lc code=end