Skip to content

Commit 4df27c8

Browse files
committed
feat: add kotlin solution to lc problem: No.0043
1 parent 91caf3b commit 4df27c8

File tree

4 files changed

+236
-0
lines changed

4 files changed

+236
-0
lines changed

solution/0000-0099/0043.Multiply Strings/README.md

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,88 @@ class Solution {
325325
}
326326
```
327327

328+
#### Kotlin
329+
330+
```kotlin
331+
class Solution {
332+
fun multiply(num1: String, num2: String): String {
333+
if (num1 == "0" || num2 == "0") return "0"
334+
335+
val chars_1 = num1.toCharArray().reversedArray()
336+
val chars_2 = num2.toCharArray().reversedArray()
337+
338+
val result = mutableListOf<Int>()
339+
340+
chars_1.forEachIndexed { i, c1 ->
341+
val multiplier_1 = c1 - '0'
342+
var over = 0
343+
var index = 0
344+
345+
fun sum(product: Int = 0): Unit {
346+
while (index >= result.size) {
347+
result.add(0)
348+
}
349+
val value = product + over + result[index]
350+
result[index] = value % 10
351+
over = value / 10
352+
return
353+
}
354+
355+
chars_2.forEachIndexed { j, c2 ->
356+
index = i + j
357+
val multiplier_2 = c2 - '0'
358+
sum(multiplier_1 * multiplier_2)
359+
}
360+
361+
while (over > 0) {
362+
index++
363+
sum()
364+
}
365+
}
366+
367+
return result.reversed().joinToString("")
368+
}
369+
}
370+
```
371+
372+
#### JavaScript
373+
374+
```js
375+
/**
376+
* @param {string} num1
377+
* @param {string} num2
378+
* @return {string}
379+
*/
380+
var multiply = function (num1, num2) {
381+
if (num1 === '0' || num2 === '0') return '0';
382+
383+
const result = Array(num1.length + num2.length).fill(0);
384+
const code_0 = '0'.charCodeAt(0);
385+
386+
const num1_len = num1.length;
387+
const num2_len = num2.length;
388+
389+
for (let i = 0; i < num1_len; ++i) {
390+
const multiplier_1 = num1.charCodeAt(num1_len - i - 1) - code_0;
391+
for (let j = 0; j < num2_len; ++j) {
392+
const multiplier_2 = num2.charCodeAt(num2_len - j - 1) - code_0;
393+
result[i + j] += multiplier_1 * multiplier_2;
394+
}
395+
}
396+
397+
result.reduce((carry, value, index) => {
398+
const sum = carry + value;
399+
result[index] = sum % 10;
400+
return (sum / 10) | 0;
401+
}, 0);
402+
403+
return result
404+
.slice(0, result.findLastIndex(d => d !== 0) + 1)
405+
.reverse()
406+
.join('');
407+
};
408+
```
409+
328410
<!-- tabs:end -->
329411

330412
<!-- solution:end -->

solution/0000-0099/0043.Multiply Strings/README_EN.md

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,88 @@ class Solution {
318318
}
319319
```
320320

321+
#### Kotlin
322+
323+
```kotlin
324+
class Solution {
325+
fun multiply(num1: String, num2: String): String {
326+
if (num1 == "0" || num2 == "0") return "0"
327+
328+
val chars_1 = num1.toCharArray().reversedArray()
329+
val chars_2 = num2.toCharArray().reversedArray()
330+
331+
val result = mutableListOf<Int>()
332+
333+
chars_1.forEachIndexed { i, c1 ->
334+
val multiplier_1 = c1 - '0'
335+
var over = 0
336+
var index = 0
337+
338+
fun sum(product: Int = 0): Unit {
339+
while (index >= result.size) {
340+
result.add(0)
341+
}
342+
val value = product + over + result[index]
343+
result[index] = value % 10
344+
over = value / 10
345+
return
346+
}
347+
348+
chars_2.forEachIndexed { j, c2 ->
349+
index = i + j
350+
val multiplier_2 = c2 - '0'
351+
sum(multiplier_1 * multiplier_2)
352+
}
353+
354+
while (over > 0) {
355+
index++
356+
sum()
357+
}
358+
}
359+
360+
return result.reversed().joinToString("")
361+
}
362+
}
363+
```
364+
365+
#### JavaScript
366+
367+
```js
368+
/**
369+
* @param {string} num1
370+
* @param {string} num2
371+
* @return {string}
372+
*/
373+
var multiply = function (num1, num2) {
374+
if (num1 === '0' || num2 === '0') return '0';
375+
376+
const result = Array(num1.length + num2.length).fill(0);
377+
const code_0 = '0'.charCodeAt(0);
378+
379+
const num1_len = num1.length;
380+
const num2_len = num2.length;
381+
382+
for (let i = 0; i < num1_len; ++i) {
383+
const multiplier_1 = num1.charCodeAt(num1_len - i - 1) - code_0;
384+
for (let j = 0; j < num2_len; ++j) {
385+
const multiplier_2 = num2.charCodeAt(num2_len - j - 1) - code_0;
386+
result[i + j] += multiplier_1 * multiplier_2;
387+
}
388+
}
389+
390+
result.reduce((carry, value, index) => {
391+
const sum = carry + value;
392+
result[index] = sum % 10;
393+
return (sum / 10) | 0;
394+
}, 0);
395+
396+
return result
397+
.slice(0, result.findLastIndex(d => d !== 0) + 1)
398+
.reverse()
399+
.join('');
400+
};
401+
```
402+
321403
<!-- tabs:end -->
322404

323405
<!-- solution:end -->
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* @param {string} num1
3+
* @param {string} num2
4+
* @return {string}
5+
*/
6+
var multiply = function (num1, num2) {
7+
if (num1 === '0' || num2 === '0') return '0';
8+
9+
const result = Array(num1.length + num2.length).fill(0);
10+
const code_0 = '0'.charCodeAt(0);
11+
12+
const num1_len = num1.length;
13+
const num2_len = num2.length;
14+
15+
for (let i = 0; i < num1_len; ++i) {
16+
const multiplier_1 = num1.charCodeAt(num1_len - i - 1) - code_0;
17+
for (let j = 0; j < num2_len; ++j) {
18+
const multiplier_2 = num2.charCodeAt(num2_len - j - 1) - code_0;
19+
result[i + j] += multiplier_1 * multiplier_2;
20+
}
21+
}
22+
23+
result.reduce((carry, value, index) => {
24+
const sum = carry + value;
25+
result[index] = sum % 10;
26+
return (sum / 10) | 0;
27+
}, 0);
28+
29+
return result
30+
.slice(0, result.findLastIndex(d => d !== 0) + 1)
31+
.reverse()
32+
.join('');
33+
};
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class Solution {
2+
fun multiply(num1: String, num2: String): String {
3+
if (num1 == "0" || num2 == "0") return "0"
4+
5+
val chars_1 = num1.toCharArray().reversedArray()
6+
val chars_2 = num2.toCharArray().reversedArray()
7+
8+
val result = mutableListOf<Int>()
9+
10+
chars_1.forEachIndexed { i, c1 ->
11+
val multiplier_1 = c1 - '0'
12+
var over = 0
13+
var index = 0
14+
15+
fun sum(product: Int = 0): Unit {
16+
while (index >= result.size) {
17+
result.add(0)
18+
}
19+
val value = product + over + result[index]
20+
result[index] = value % 10
21+
over = value / 10
22+
return
23+
}
24+
25+
chars_2.forEachIndexed { j, c2 ->
26+
index = i + j
27+
val multiplier_2 = c2 - '0'
28+
sum(multiplier_1 * multiplier_2)
29+
}
30+
31+
while (over > 0) {
32+
index++
33+
sum()
34+
}
35+
}
36+
37+
return result.reversed().joinToString("")
38+
}
39+
}

0 commit comments

Comments
 (0)