Skip to content

Commit ae3b1c2

Browse files
aQuaaQua
authored andcommitted
823 简化了代码
1 parent 91a5ce0 commit ae3b1c2

File tree

1 file changed

+10
-19
lines changed

1 file changed

+10
-19
lines changed
Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package problem0823
22

3-
import (
4-
"sort"
5-
)
3+
import "sort"
64

75
const (
86
modulo = 1E9 + 7
@@ -17,35 +15,24 @@ func numFactoredBinaryTrees(A []int) int {
1715
}
1816

1917
ress := make([]int, len(A))
20-
for i := range ress {
21-
ress[i] = 1
22-
}
2318

24-
for i := 1; i < len(A); i++ {
19+
for i := 0; i < len(A); i++ {
20+
ress[i] = 1
2521
for j := 0; j < i; j++ {
2622
quotient, remainder := A[i]/A[j], A[i]%A[j]
23+
k, isFactor := factorIndex[quotient]
2724

28-
if remainder != 0 ||
29-
(factorIndex[quotient] == 0 && quotient != A[0]) {
25+
if remainder != 0 || !isFactor {
3026
continue
3127
}
3228

33-
k := factorIndex[quotient]
34-
35-
t := mod(ress[j] * ress[k])
36-
37-
ress[i] = mod(ress[i] + t)
38-
29+
ress[i] = mod(ress[i] + ress[j]*ress[k])
3930
}
4031
}
4132

4233
return sum(ress)
4334
}
4435

45-
func mod(n int) int {
46-
return n % modulo
47-
}
48-
4936
func sum(a []int) int {
5037
res := 0
5138
for i := range a {
@@ -54,3 +41,7 @@ func sum(a []int) int {
5441
}
5542
return res
5643
}
44+
45+
func mod(n int) int {
46+
return n % modulo
47+
}

0 commit comments

Comments
 (0)