add new solutions
This commit is contained in:
parent
489fa73880
commit
0f5f9e331c
11 changed files with 539 additions and 0 deletions
49
solutions/0/q43/solution.go
Normal file
49
solutions/0/q43/solution.go
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
package q43
|
||||
|
||||
import "slices"
|
||||
|
||||
func multiply(num1, num2 string) string {
|
||||
if num1 == "0" || num2 == "0" {
|
||||
return "0"
|
||||
}
|
||||
|
||||
l1, l2 := len(num1), len(num2)
|
||||
res := make([]byte, l1+l2)
|
||||
|
||||
// Calculation
|
||||
for i1 := range l1 {
|
||||
n1 := num1[l1-1-i1] - '0'
|
||||
if n1 == 0 {
|
||||
continue
|
||||
}
|
||||
for i2 := range l2 {
|
||||
n2 := num2[l2-1-i2] - '0'
|
||||
j := i1 + i2
|
||||
res[j] += n1 * n2
|
||||
if res[j] > 9 {
|
||||
res[j+1] += res[j] / 10
|
||||
res[j] = res[j] % 10
|
||||
}
|
||||
}
|
||||
for i := l2 + i1; res[i] > 9; i++ {
|
||||
res[i+1] += res[i] / 10
|
||||
res[i] = res[i] % 10
|
||||
}
|
||||
}
|
||||
|
||||
// Post process
|
||||
for i := len(res) - 1; i >= 0; i-- {
|
||||
if res[i] > 0 {
|
||||
res = res[:i+1]
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
slices.Reverse(res)
|
||||
for i := range res {
|
||||
res[i] = res[i] + '0'
|
||||
}
|
||||
return string(res)
|
||||
}
|
||||
|
||||
var _ = multiply
|
||||
Loading…
Add table
Add a link
Reference in a new issue