Skip to content

Commit 83b0b7a

Browse files
committed
chore: multiply strings
1 parent 63a50a9 commit 83b0b7a

File tree

3 files changed

+197
-28
lines changed

3 files changed

+197
-28
lines changed

multiply_string.go

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
/*
8+
*
9+
Given two non-negative integers num1 and num2 represented as strings,
10+
return the product of num1 and num2, also represented as a string.
11+
Note: You must not use any built-in BigInteger library or convert the inputs to integer directly.
12+
*/
13+
14+
// worst case could cause integer overflow...
15+
// O(n)
16+
// for int 64
17+
// 498828660196 * 498828660196 = -3269442614257959980 // integer overflow right here
18+
func Multiply(num1 string, num2 string) string {
19+
strMap := map[string]int{
20+
"0": 0,
21+
"1": 1,
22+
"2": 2,
23+
"3": 3,
24+
"4": 4,
25+
"5": 5,
26+
"6": 6,
27+
"7": 7,
28+
"8": 8,
29+
"9": 9,
30+
}
31+
var va int64
32+
var vap int64
33+
34+
num1Len := len(num1)
35+
num2Len := len(num2)
36+
37+
i, j := 0, 0
38+
39+
for i < num1Len || j < num2Len {
40+
if i < num1Len {
41+
va = va*10 + int64(strMap[string(num1[i])])
42+
}
43+
if j < num2Len {
44+
vap = vap*10 + int64(strMap[string(num2[j])])
45+
}
46+
i++
47+
j++
48+
}
49+
return fmt.Sprintf("%d", va*vap)
50+
}
51+
52+
func Multiply2(num1 string, num2 string) string {
53+
54+
if num1 == "0" || num2 == "0" {
55+
return "0"
56+
}
57+
58+
n, m := len(num1), len(num2)
59+
result := make([]int, n+m)
60+
for i := n - 1; i >= 0; i-- {
61+
for j := m - 1; j >= 0; j-- {
62+
d1 := int(num1[i] - '0')
63+
d2 := int(num2[j] - '0')
64+
mul := d1 * d2
65+
p1, p2 := i+j, i+j+1
66+
sum := mul + result[p2]
67+
result[p2] = sum % 10
68+
result[p1] += sum / 10
69+
}
70+
}
71+
res := ""
72+
for _, digit := range result {
73+
if !(len(res) == 0 && digit == 0) {
74+
res += fmt.Sprint(digit)
75+
}
76+
}
77+
return res
78+
}
79+
80+
func main() {
81+
fmt.Println(Multiply2("498828660196", "840477629533"))
82+
}
83+
84+
/*
85+
*
86+
87+
apparently this function is illegal to use
88+
sybau...
89+
*/
90+
func convertToInt(str string) int {
91+
if str == "" || str == " " || len(str) == 0 {
92+
return 0
93+
}
94+
95+
num := 0
96+
sign := 1
97+
98+
if str[0] == '-' {
99+
sign = -1
100+
str = str[1:]
101+
}
102+
103+
for _, c := range str {
104+
switch c {
105+
case '0':
106+
num = (num * 10)
107+
case '1':
108+
num = (num * 10) + 1
109+
case '2':
110+
num = (num * 10) + 2
111+
case '3':
112+
num = (num * 10) + 3
113+
case '4':
114+
num = (num * 10) + 4
115+
case '5':
116+
num = (num * 10) + 5
117+
case '6':
118+
num = (num * 10) + 6
119+
case '7':
120+
num = (num * 10) + 7
121+
case '8':
122+
num = (num * 10) + 8
123+
case '9':
124+
num = (num * 10) + 9
125+
default:
126+
fmt.Println("bleh bleh bleeeeh bleeeeeeeeeeeh")
127+
return 10 * 10 * 10 * 10 * 10 * 10 * 10 * 10 * 10 * 10 * 10 * 10 * 10 * 10 // don't ask me why this here...
128+
}
129+
}
130+
return num * sign
131+
}

multiply_string_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package main
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestMultiply(t *testing.T) {
8+
9+
var additiveTest = []struct {
10+
a string
11+
b string
12+
expect string
13+
}{
14+
{
15+
a: "1",
16+
b: "2",
17+
expect: "2",
18+
},
19+
{
20+
a: "123",
21+
b: "456",
22+
expect: "56088",
23+
},
24+
{
25+
a: "498828660196",
26+
b: "840477629533",
27+
expect: "419254329864656431168468",
28+
},
29+
}
30+
31+
for _, mul := range additiveTest {
32+
got := Multiply2(mul.a, mul.b)
33+
if mul.expect != got {
34+
t.Errorf("expected %s got %s in test multiply string", mul.expect, got)
35+
}
36+
37+
}
38+
}

two_numbers.go

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -68,35 +68,35 @@ func (l *ListNode) PrintNode() {
6868
fmt.Println()
6969
}
7070

71-
func main() {
72-
node2 := Node{}
73-
Lnn := &ListNode{Val: 9}
74-
Lnn1 := &ListNode{Val: 9}
75-
Lnn2 := &ListNode{Val: 9}
76-
Lnn3 := &ListNode{Val: 9}
77-
Lnn4 := &ListNode{Val: 9}
78-
Lnn5 := &ListNode{Val: 9}
79-
Lnn6 := &ListNode{Val: 9}
71+
// func main() {
72+
// node2 := Node{}
73+
// Lnn := &ListNode{Val: 9}
74+
// Lnn1 := &ListNode{Val: 9}
75+
// Lnn2 := &ListNode{Val: 9}
76+
// Lnn3 := &ListNode{Val: 9}
77+
// Lnn4 := &ListNode{Val: 9}
78+
// Lnn5 := &ListNode{Val: 9}
79+
// Lnn6 := &ListNode{Val: 9}
8080

81-
node2.Prepend(Lnn)
82-
node2.Prepend(Lnn1)
83-
node2.Prepend(Lnn2)
84-
node2.Prepend(Lnn3)
85-
node2.Prepend(Lnn4)
86-
node2.Prepend(Lnn5)
87-
node2.Prepend(Lnn6)
81+
// node2.Prepend(Lnn)
82+
// node2.Prepend(Lnn1)
83+
// node2.Prepend(Lnn2)
84+
// node2.Prepend(Lnn3)
85+
// node2.Prepend(Lnn4)
86+
// node2.Prepend(Lnn5)
87+
// node2.Prepend(Lnn6)
8888

89-
node := Node{}
90-
Ln := &ListNode{Val: 9}
91-
Ln1 := &ListNode{Val: 9}
92-
Ln2 := &ListNode{Val: 9}
93-
Ln3 := &ListNode{Val: 9}
89+
// node := Node{}
90+
// Ln := &ListNode{Val: 9}
91+
// Ln1 := &ListNode{Val: 9}
92+
// Ln2 := &ListNode{Val: 9}
93+
// Ln3 := &ListNode{Val: 9}
9494

95-
node.Prepend(Ln)
96-
node.Prepend(Ln1)
97-
node.Prepend(Ln2)
98-
node.Prepend(Ln3)
95+
// node.Prepend(Ln)
96+
// node.Prepend(Ln1)
97+
// node.Prepend(Ln2)
98+
// node.Prepend(Ln3)
9999

100-
a := addTwoNumbers(node2.Head, node.Head)
101-
a.PrintNode()
102-
}
100+
// a := addTwoNumbers(node2.Head, node.Head)
101+
// a.PrintNode()
102+
// }

0 commit comments

Comments
 (0)