trie
trie.go 源码
package leetcode
// 实现前缀树
type Trie struct {
end bool
children [26]*Trie
}
func Constructor() Trie {
return Trie{end: false}
}
func (this *Trie) Insert(word string) {
p := this
for _, w := range word {
if p.children[w-'a'] == nil {
p.children[w-'a'] = &Trie{end: false}
}
p = p.children[w-'a']
}
p.end = true
}
func (this *Trie) Search(word string) bool {
p := this
for _, w := range word {
if p.children[w-'a'] == nil {
return false
}
p = p.children[w-'a']
}
return p.end
}
func (this *Trie) StartsWith(prefix string) bool {
p := this
for _, w := range prefix {
if p.children[w-'a'] == nil {
return false
}
p = p.children[w-'a']
}
return true
}
你可能感兴趣的文章
0
赞
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦