Skip to content

Commit 4056779

Browse files
committed
fix global map
1 parent 8723380 commit 4056779

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

basic_algorithm/dp.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -486,36 +486,37 @@ func wordBreak(s string, wordDict []string) bool {
486486
}
487487
f := make([]bool, len(s)+1)
488488
f[0] = true
489-
max := maxLen(wordDict)
489+
max,dict := maxLen(wordDict)
490490
for i := 1; i <= len(s); i++ {
491491
l := 0
492492
if i - max > 0 {
493493
l = i - max
494494
}
495495
for j := l; j < i; j++ {
496-
if f[j] && inDict(s[j:i]) {
496+
if f[j] && inDict(s[j:i],dict) {
497497
f[i] = true
498-
break
498+
break
499499
}
500500
}
501501
}
502502
return f[len(s)]
503503
}
504504

505-
var dict = make(map[string]bool)
506505

507-
func maxLen(wordDict []string) int {
506+
507+
func maxLen(wordDict []string) (int,map[string]bool) {
508+
dict := make(map[string]bool)
508509
max := 0
509510
for _, v := range wordDict {
510511
dict[v] = true
511512
if len(v) > max {
512513
max = len(v)
513514
}
514515
}
515-
return max
516+
return max,dict
516517
}
517518

518-
func inDict(s string) bool {
519+
func inDict(s string,dict map[string]bool) bool {
519520
_, ok := dict[s]
520521
return ok
521522
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy