問題:html
Implement a trie with insert
, search
, and startsWith
methods.數組
Note:
You may assume that all inputs are consist of lowercase letters a-z
.數據結構
解決:https://www.cnblogs.com/grandyang/p/4491665.html性能
【題意】實現字典樹(前綴樹),這道題讓咱們實現一個重要但又有些複雜的數據結構-字典樹, 又稱前綴樹或單詞查找樹,例如,一個保存了8個鍵的trie結構,"A", "to", "tea", "ted", "ten", "i", "in", and "inn".以下圖所示:this
字典樹主要有以下三點性質:spa
1. 根節點不包含字符,除根節點之外每一個節點只包含一個字符。code
2. 從根節點到某一個節點,路徑上通過的字符鏈接起來,爲該節點對應的字符串。htm
3. 每一個節點的全部子節點包含的字符串不相同。blog
① 一個字典樹的節點應該包含字母,其子節點,以及標記其是否爲葉節點的標記位。ip
class TrieNode{//209ms
char c;//當前節點的字符
Map<Character,TrieNode> children = new HashMap<>();//子節點
boolean isLeaf;//標記是否爲葉節點
public TrieNode(){}
public TrieNode(char c){
this.c = c;
}
}
public class Trie{
private TrieNode root;
public Trie(){
root = new TrieNode();//初始化根節點
}
public void insert(String word){
Map<Character,TrieNode> cur = root.children;//獲取根節點的子節點,從它開始加入
for (int i = 0;i < word.length() ;i ++ ) {
char c = word.charAt(i);
TrieNode tmp;
if (cur.containsKey(c)) {
tmp = cur.get(c);//獲取插入的節點
}else{
tmp = new TrieNode(c);
cur.put(c,tmp);
}
cur = tmp.children;
if (i == word.length() - 1) {
tmp.isLeaf = true;
}
}
}
public boolean search(String word){
TrieNode tmp = searchNode(word);//查找前綴,並返回葉節點
if (tmp != null && tmp.isLeaf) {
return true;
}else{
return false;
}
}
public boolean startsWith(String prefix){
if (searchNode(prefix) == null) {
return false;
}else{
return true;
}
}
public TrieNode searchNode(String str){//查找前綴
Map<Character,TrieNode> cur = root.children;
TrieNode tmp = null;
for (int i = 0;i < str.length() ;i ++ ) {
char c = str.charAt(i);
if (cur.containsKey(c)) {
tmp = cur.get(c);
cur = tmp.children;
}else{
return null;
}
}
return tmp;
}
}
/**
* 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);
*/
② 使用數組提升性能。
每一個trie節點只能包含'a' - 'z'字符。 因此咱們能夠使用一個數組來存儲這個字符。
class TrieNode{//174ms
TrieNode[] children;//每一個節點用一個大小爲26的數組表示,由於小寫字母範圍爲a-z
boolean isLeaf;
public TrieNode(){
children = new TrieNode[26];//初始化節點
}
}
public class Trie{
private TrieNode root;
public Trie(){
root = new TrieNode();//初始化根節點
}
public void insert(String word){
TrieNode cur = root;
for (int i = 0;i < word.length() ;i ++ ) {
char c = word.charAt(i);
int index = c - 'a';
if (cur.children[index] != null) {
cur = cur.children[index];
}else{
TrieNode tmp = new TrieNode();
cur.children[index] = tmp;
cur = tmp;
}
}
cur.isLeaf = true;
}
public boolean search(String word){
TrieNode tmp = searchNode(word);//查找前綴,並返回最後一個字符所在的節點
if (tmp != null && tmp.isLeaf) {
return true;
}
return false;
}
public boolean startsWith(String prefix){
if (searchNode(prefix) == null) {
return false;
}else{
return true;
}
}
public TrieNode searchNode(String str){
TrieNode cur = root;
for (int i = 0;i < str.length() ;i ++ ) {
char c = str.charAt(i);
int index = c - 'a';
if (cur.children[index] != null) {
cur = cur.children[index];
}else{
return null;
}
}
if (cur == root) {
return null;
}
return cur;
}
}
/**
* 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);
*/
③ 將TrieNode做爲內部類。
class Trie{ //160ms class TrieNode{ TrieNode[] children; boolean isLeaf; public TrieNode(){ children = new TrieNode[26]; isLeaf = false; } } private TrieNode root; public Trie(){ root = new TrieNode(); } public void insert(String word){ TrieNode cur = root; char[] wchar = word.toCharArray(); for (int i = 0;i < word.length() ;i ++ ) { if (cur.children[wchar[i] - 'a'] != null) { cur = cur.children[wchar[i] - 'a']; }else{ TrieNode tmp = new TrieNode(); cur.children[wchar[i] - 'a'] = tmp; cur = tmp; } } cur.isLeaf = true; } public boolean search(String word){ char[] wchar = word.toCharArray(); TrieNode cur = root; for (int i = 0;i < word.length() ;i ++ ) { if (cur.children[wchar[i] - 'a'] == null) { return false; }else{ cur = cur.children[wchar[i] - 'a']; } } return cur.isLeaf; } public boolean startsWith(String word){ if (word.length() == 0) { return true; } char[] wchar = word.toCharArray(); TrieNode cur = root; for (int i = 0;i < word.length() ;i ++ ) { if (cur.children[wchar[i] - 'a'] == null) { return false; }else{ cur = cur.children[wchar[i] - '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); */