mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-05 00:25:22 +08:00
规范格式
This commit is contained in:
@ -0,0 +1,60 @@
|
||||
package leetcode
|
||||
|
||||
type Trie struct {
|
||||
isWord bool
|
||||
children map[rune]*Trie
|
||||
}
|
||||
|
||||
/** Initialize your data structure here. */
|
||||
func Constructor208() Trie {
|
||||
return Trie{isWord: false, children: make(map[rune]*Trie)}
|
||||
}
|
||||
|
||||
/** Inserts a word into the trie. */
|
||||
func (this *Trie) Insert(word string) {
|
||||
parent := this
|
||||
for _, ch := range word {
|
||||
if child, ok := parent.children[ch]; ok {
|
||||
parent = child
|
||||
} else {
|
||||
newChild := &Trie{children: make(map[rune]*Trie)}
|
||||
parent.children[ch] = newChild
|
||||
parent = newChild
|
||||
}
|
||||
}
|
||||
parent.isWord = true
|
||||
}
|
||||
|
||||
/** Returns if the word is in the trie. */
|
||||
func (this *Trie) Search(word string) bool {
|
||||
parent := this
|
||||
for _, ch := range word {
|
||||
if child, ok := parent.children[ch]; ok {
|
||||
parent = child
|
||||
continue
|
||||
}
|
||||
return false
|
||||
}
|
||||
return parent.isWord
|
||||
}
|
||||
|
||||
/** Returns if there is any word in the trie that starts with the given prefix. */
|
||||
func (this *Trie) StartsWith(prefix string) bool {
|
||||
parent := this
|
||||
for _, ch := range prefix {
|
||||
if child, ok := parent.children[ch]; ok {
|
||||
parent = child
|
||||
continue
|
||||
}
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
/**
|
||||
* Your Trie object will be instantiated and called as such:
|
||||
* obj := Constructor();
|
||||
* obj.Insert(word);
|
||||
* param_2 := obj.Search(word);
|
||||
* param_3 := obj.StartsWith(prefix);
|
||||
*/
|
@ -0,0 +1,23 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_Problem208(t *testing.T) {
|
||||
obj := Constructor208()
|
||||
fmt.Printf("obj = %v\n", obj)
|
||||
obj.Insert("apple")
|
||||
fmt.Printf("obj = %v\n", obj)
|
||||
param1 := obj.Search("apple")
|
||||
fmt.Printf("param_1 = %v obj = %v\n", param1, obj)
|
||||
param2 := obj.Search("app")
|
||||
fmt.Printf("param_2 = %v obj = %v\n", param2, obj)
|
||||
param3 := obj.StartsWith("app")
|
||||
fmt.Printf("param_3 = %v obj = %v\n", param3, obj)
|
||||
obj.Insert("app")
|
||||
fmt.Printf("obj = %v\n", obj)
|
||||
param4 := obj.Search("app")
|
||||
fmt.Printf("param_4 = %v obj = %v\n", param4, obj)
|
||||
}
|
31
leetcode/0208.Implement-Trie-Prefix-Tree/README.md
Executable file
31
leetcode/0208.Implement-Trie-Prefix-Tree/README.md
Executable file
@ -0,0 +1,31 @@
|
||||
# [208. Implement Trie (Prefix Tree)](https://leetcode.com/problems/implement-trie-prefix-tree/)
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
Implement a trie with `insert`, `search`, and `startsWith` methods.
|
||||
|
||||
**Example:**
|
||||
|
||||
Trie trie = new Trie();
|
||||
|
||||
trie.insert("apple");
|
||||
trie.search("apple"); // returns true
|
||||
trie.search("app"); // returns false
|
||||
trie.startsWith("app"); // returns true
|
||||
trie.insert("app");
|
||||
trie.search("app"); // returns true
|
||||
|
||||
**Note:**
|
||||
|
||||
- You may assume that all inputs are consist of lowercase letters `a-z`.
|
||||
- All inputs are guaranteed to be non-empty strings.
|
||||
|
||||
## 题目大意
|
||||
|
||||
实现一个 Trie (前缀树),包含 insert, search, 和 startsWith 这三个操作。
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 要求实现一个 Trie 的数据结构,具有 `insert`, `search`, `startsWith` 三种操作
|
||||
- 这一题就是经典的 Trie 实现。本题的实现可以作为 Trie 的模板。
|
Reference in New Issue
Block a user