-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAddToArrayForm.go
165 lines (118 loc) · 2.78 KB
/
AddToArrayForm.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
For a non-negative integer X, the array-form of X is an array of its digits in left to right order. For example, if X = 1231, then the array form is [1,2,3,1].
Given the array-form A of a non-negative integer X, return the array-form of the integer X+K.
Example 1:
Input: A = [1,2,0,0], K = 34
Output: [1,2,3,4]
Explanation: 1200 + 34 = 1234
Example 2:
Input: A = [2,7,4], K = 181
Output: [4,5,5]
Explanation: 274 + 181 = 455
Example 3:
Input: A = [2,1,5], K = 806
Output: [1,0,2,1]
Explanation: 215 + 806 = 1021
Example 4:
Input: A = [9,9,9,9,9,9,9,9,9,9], K = 1
Output: [1,0,0,0,0,0,0,0,0,0,0]
Explanation: 9999999999 + 1 = 10000000000
Note:
1 <= A.length <= 10000
0 <= A[i] <= 9
0 <= K <= 10000
If A.length > 1, then A[0] != 0
*/
package main
import (
"log"
"math"
)
func main() {
// log.Println(int(math.Pow10(0)))
// tests := [][]int{ {1,2,3}, {2,1}, {0} }
// for _, test := range tests {
// log.Printf("arrayToInt(%v) = %d\n", test, arrayToInt(test))
// }
// tests := []int{321, 0}
// for _, test := range tests {
// log.Printf("intToArray(%v) = %d\n", test, intToArray(test))
// }
tests := [][][]int{ {{1,2,0,0},{34}}, {{2,7,4}, {181}}, {{2,1,5}, {806}}, {{9,9,9,9,9,9,9,9,9,9},{1}}, {{0},{0}} }
for _, test := range tests {
// log.Printf("addToArrayForm(%v, %d) = %v\n", test[0], test[1][0], addToArrayForm(test[0], test[1][0]))
log.Printf("addToArrayForm(%v, %d) = %v\n", test[0], test[1][0], addToArrayForm2(test[0], test[1][0]))
}
}
func addToArrayForm2(A []int, K int) []int {
i := 1
x := true
y := true
K_arr := intToArray(K)
var res int
var rem int
resArr := []int{}
for x || y {
x1 := 0
x2 := 0
if (len(A)-i) >= 0 {
x1 = A[len(A)-i]
} else {
x = false
}
if (len(K_arr)-i) >= 0 {
x2 = K_arr[len(K_arr)-i]
} else {
y = false
}
i++
res = x1 + x2 + rem
if res >= 10 {
rem = res % 10
res = res / 10
} else {
rem = 0
}
resArr = append(resArr, res)
}
if rem > 0 {
resArr = append(resArr, rem)
}
return reverse(resArr)
}
func addToArrayForm(A []int, K int) []int {
// return A
return intToArray(arrayToInt(A) + K)
}
func reverse(x []int) []int {
for i := 0; i < len(x)/2; i++ {
x[i], x[len(x)-i-1] = x[len(x)-i-1], x[i]
}
return x
}
func intToArray(x int) []int {
res := []int{}
var rem int
if x == 0 {
return []int{0}
}
for x > 0 {
rem = x % 10
x = x / 10
res = append(res, rem)
}
return reverse(res)
}
// turn an integer given as an array of its decimal digits to int form
func arrayToInt(x []int) int {
res := 0
for i := len(x) - 1; i >= 0; i-- {
// log.Println("--------------------------")
// log.Println(math.Pow10(x[len(x)-i-1]))
// log.Println(res)
// log.Println(x[i])
res += x[i] * int(math.Pow10(len(x)-i-1))
// log.Println(res)
}
return res
}