Skip to content

Commit d080c8f

Browse files
aQuaaQua
authored andcommitted
757 修改为闭包
1 parent 047a826 commit d080c8f

File tree

1 file changed

+24
-24
lines changed

1 file changed

+24
-24
lines changed

Algorithms/0757.pyramid-transition-matrix/pyramid-transition-matrix.go

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,33 @@ import (
77
func pyramidTransition(bottom string, allowed []string) bool {
88
blocks := "#" + strings.Join(allowed, "#")
99

10-
return helper(bottom, blocks, 0, len(bottom))
11-
}
12-
13-
func helper(bottom, blocks string, curr, length int) bool {
14-
if curr+2 > length {
15-
bottom = bottom[length:]
16-
length = len(bottom)
17-
if length == 1 {
18-
return true
10+
var helper func(string, string, int, int) bool
11+
helper = func(bottom, blocks string, curr, length int) bool {
12+
if curr+2 > length {
13+
bottom = bottom[length:]
14+
length = len(bottom)
15+
if length == 1 {
16+
return true
17+
}
18+
return helper(bottom, blocks, 0, length)
1919
}
20-
return helper(bottom, blocks, 0, length)
21-
}
2220

23-
b := "#" + bottom[curr:curr+2]
24-
25-
count := strings.Count(blocks, b)
26-
27-
for i := 0; i < count; i++ {
28-
index := strings.Index(blocks, b)
29-
blk := blocks[index : index+4]
30-
color := blk[3:]
31-
blocks = strings.Replace(blocks, blk, "", 1)
32-
blocks += blk
33-
if helper(bottom+color, blocks, curr+1, length) {
34-
return true
21+
b := "#" + bottom[curr:curr+2]
22+
beg := 0
23+
for beg < len(blocks) {
24+
index := strings.Index(blocks[beg:], b) + beg
25+
if index < beg {
26+
break
27+
}
28+
beg = index + 4
29+
color := blocks[index+3 : index+4]
30+
if helper(bottom+color, blocks, curr+1, length) {
31+
return true
32+
}
3533
}
34+
35+
return false
3636
}
3737

38-
return false
38+
return helper(bottom, blocks, 0, len(bottom))
3939
}

0 commit comments

Comments
 (0)