Skip to content

Commit ff104fb

Browse files
committed
module github.com/masx200/leetcode-test/abbreviating-the-product-of-a-range
1 parent 100ca30 commit ff104fb

File tree

4 files changed

+50
-1
lines changed

4 files changed

+50
-1
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package index
2+
3+
func AbbreviateProduct(left, right int) string {
4+
5+
return abbreviateProduct(left, right)
6+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module github.com/masx200/leetcode-test/abbreviating-the-product-of-a-range
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package index
2+
3+
import (
4+
"fmt"
5+
"math/big"
6+
"strconv"
7+
"strings"
8+
)
9+
10+
var factorials = make([]*big.Int, 10001)
11+
12+
func initialization() {
13+
if factorials[0] == nil {
14+
var i int64 = 0
15+
for ; i <= 10000; i++ {
16+
if i == 0 {
17+
factorials[i] = big.NewInt(1)
18+
} else {
19+
factorials[i] = new(big.Int).Mul(factorials[i-1], big.NewInt(i))
20+
}
21+
22+
}
23+
}
24+
}
25+
26+
func abbreviateProduct(left, right int) string {
27+
initialization()
28+
var s string
29+
if left == right {
30+
s = strconv.Itoa(left)
31+
} else {
32+
s = new(big.Int).Div(factorials[right], factorials[left-1]).String()
33+
}
34+
tz := len(s)
35+
s = strings.TrimRight(s, "0")
36+
tz -= len(s)
37+
if len(s) > 10 {
38+
return fmt.Sprintf("%s...%se%d", s[:5], s[len(s)-5:], tz)
39+
}
40+
return fmt.Sprintf("%se%d", s, tz)
41+
}

go.work

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
go 1.19
22

33
use (
4+
./abbreviating-the-product-of-a-range
45
.
56
./check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence
67
./counting-words-with-a-given-prefix
@@ -16,5 +17,5 @@ use (
1617
./serialize-and-deserialize-binary-tree
1718
./transform-to-chessboard
1819
./utils
19-
./count-complete-tree-nodes
20+
./count-complete-tree-nodes
2021
)

0 commit comments

Comments
 (0)