-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
Copy pathSolution.kt
39 lines (32 loc) · 1.05 KB
/
Solution.kt
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
class Solution {
fun multiply(num1: String, num2: String): String {
if (num1 == "0" || num2 == "0") return "0"
val chars_1 = num1.toCharArray().reversedArray()
val chars_2 = num2.toCharArray().reversedArray()
val result = mutableListOf<Int>()
chars_1.forEachIndexed { i, c1 ->
val multiplier_1 = c1 - '0'
var over = 0
var index = 0
fun sum(product: Int = 0): Unit {
while (index >= result.size) {
result.add(0)
}
val value = product + over + result[index]
result[index] = value % 10
over = value / 10
return
}
chars_2.forEachIndexed { j, c2 ->
index = i + j
val multiplier_2 = c2 - '0'
sum(multiplier_1 * multiplier_2)
}
while (over > 0) {
index++
sum()
}
}
return result.reversed().joinToString("")
}
}