208. 實現(xiàn) Trie (前綴樹)
實現(xiàn)一個 Trie (前綴樹)尤莺,包含 insert, search, 和 startsWith 這三個操作。
示例:
Trie trie = new Trie();
trie.insert("apple");
trie.search("apple"); // 返回 true
trie.search("app"); // 返回 false
trie.startsWith("app"); // 返回 true
trie.insert("app");
trie.search("app"); // 返回 true
說明:
你可以假設所有的輸入都是由小寫字母 a-z 構成的毙驯。
保證所有輸入均為非空字符串掺出。
在真實的面試中遇到過這道題?
Python3 實現(xiàn)
# @author:leacoder
# @des: 實現(xiàn) Trie (前綴樹)
class Trie:
def __init__(self):
"""
Initialize your data structure here.
"""
self.root = {}
self.endofword = "end"
def insert(self, word: str) -> None:
"""
Inserts a word into the trie.
"""
node = self.root
for char in word:
# dict.setdefault(key, default=None) 如果 key 在 字典中掉冶,返回對應的值被环。
# 如果不在字典中糙及,則插入 key 及設置的默認值 default,并返回 default 筛欢,default 默認值為 None浸锨。
node = node.setdefault(char,{})
node[self.endofword] = self.endofword
def search(self, word: str) -> bool:
"""
Returns if the word is in the trie.
"""
node = self.root
for char in word:
if char not in node:
return False
#node = node[char]
node = node.get(char)
return self.endofword in node
def startsWith(self, prefix: str) -> bool:
"""
Returns if there is any word in the trie that starts with the given prefix.
"""
node = self.root
for char in prefix:
if char not in node:
return False
#node = node[char]
node = node.get(char)
return True
Java 實現(xiàn)
/*
*@author:leacoder
*@des: 實現(xiàn) Trie (前綴樹)
*/
class Trie {
public int SIZE = 26;
public TrieNode root;
class TrieNode {
TrieNode(char c){
this.val = c;
this.isWord = false;
this.child = new TrieNode[SIZE];
}
public char val;
public boolean isWord;
public TrieNode[] child ;
}
/** Initialize your data structure here. */
public Trie() {
this.root = new TrieNode(' ');
}
/** Inserts a word into the trie. */
public void insert(String word) {
if(word == null || word.length() == 0) return;
TrieNode node = this.root;
for( int i = 0; i < word.length(); i++ ){
char c = word.charAt(i);
if( node.child[c - 'a'] == null){
node.child[c - 'a'] = new TrieNode(c);
}
node = node.child[c - 'a'];
}
node.isWord = true;
}
/** Returns if the word is in the trie. */
public boolean search(String word) {
TrieNode node = this.root;
for( int i = 0; i < word.length(); i++ ){
char c = word.charAt(i);
if( node.child[c - 'a'] == null){
return false;
}
node = node.child[c - 'a'];
}
return node.isWord;
}
/** Returns if there is any word in the trie that starts with the given prefix. */
public boolean startsWith(String prefix) {
TrieNode node = this.root;
for( int i = 0; i < prefix.length(); i++ ){
char c = prefix.charAt(i);
if( node.child[c - 'a'] == null){
return false;
}
node = node.child[c - 'a'];
}
return true;
}
}
/**
* Your Trie object will be instantiated and called as such:
* Trie obj = new Trie();
* obj.insert(word);
* boolean param_2 = obj.search(word);
* boolean param_3 = obj.startsWith(prefix);
*/
GitHub鏈接:
https://github.com/lichangke/LeetCode
知乎個人首頁:
https://www.zhihu.com/people/lichangke/
簡書個人首頁:
http://www.reibang.com/u/3e95c7555dc7
個人Blog:
https://lichangke.github.io/
歡迎大家來一起交流學習