-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathMultiplyStrings.go
More file actions
53 lines (45 loc) 路 1.24 KB
/
Copy pathMultiplyStrings.go
File metadata and controls
53 lines (45 loc) 路 1.24 KB
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
package MultiplyStrings
import (
"strconv"
"strings"
)
//Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2.
//
//Note:
//
//The length of both num1 and num2 is < 110.
//Both num1 and num2 contains only digits 0-9.
//Both num1 and num2 does not contain any leading zero.
//You must not use any built-in BigInteger library or convert the inputs to integer directly.
//
//Accepted.
func multiply(num1 string, num2 string) string {
num1Length, num2Length := len(num1), len(num2)
ints := make([]int, num1Length+num2Length)
for i := num1Length - 1; i >= 0; i-- {
for j := num2Length - 1; j >= 0; j-- {
ints[(num1Length-i-1)+(num2Length-j-1)] += int(num1[i]-'0') * int(num2[j]-'0')
}
}
res := make([]string, 0)
for i := 0; i < num1Length+num2Length; i++ {
digit := ints[i] % 10
carry := ints[i] / 10
tmp := make([]string, 1)
tmp[0] = strconv.Itoa(digit)
res = append(tmp, res...)
if i < (num1Length + num2Length - 1) {
ints[i+1] += carry
}
}
for ; len(res) > 0 && res[0] == "0"; {
res = removeIndex(res, 0)
}
if len(res) == 0 {
return "0"
}
return strings.Join(res, "")
}
func removeIndex(s []string, index int) []string {
return append(s[:index], s[index+1:]...)
}