Skip to content

Commit 3647270

Browse files
authored
Create 0043-multiply-strings.kt
1 parent 19ac281 commit 3647270

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

kotlin/0043-multiply-strings.kt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
class Solution {
3+
fun multiply(num1: String, num2: String): String {
4+
if (num1=="0" || num2=="0") return "0"
5+
6+
val sum = IntArray(num1.length + num2.length)
7+
val n1 = num1.reversed()
8+
val n2 = num2.reversed()
9+
10+
for (i in 0 until n1.length) {
11+
for (j in 0 until n2.length) {
12+
var temp = (n1[i] - '0') * (n2[j] - '0') + sum[i + j]
13+
sum[i + j + 1] += temp / 10
14+
sum[i + j] = temp % 10
15+
}
16+
}
17+
18+
return StringBuilder().apply {
19+
for(i in sum.lastIndex downTo 0) {
20+
if (this.length == 0 && sum[i] == 0) continue
21+
this.append(sum[i])
22+
}
23+
}.toString()
24+
25+
}
26+
}

0 commit comments

Comments
 (0)