-
Notifications
You must be signed in to change notification settings - Fork 11
/
208.实现-trie-前缀树.java
73 lines (61 loc) · 1.83 KB
/
208.实现-trie-前缀树.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
* @lc app=leetcode.cn id=208 lang=java
*
* [208] 实现 Trie (前缀树)
*/
// @lc code=start
class TrieNode{
TrieNode[] child;
boolean is_end;
public TrieNode(){
child = new TrieNode[26];
is_end = false;
}
}
class Trie {
TrieNode root ; // 记录根节点
public Trie() {
root = new TrieNode();
}
public void insert(String word) {
TrieNode ptr = root; // 从根出发
for (int i = 0; i < word.length(); i++) {
char c = word.charAt(i); // 对于每个单词
if(ptr.child[c-'a'] == null){ // 说明为空没有插入
ptr.child[c-'a'] = new TrieNode(); //存入新的节点
}
ptr = ptr.child[c-'a']; // 指向下一个新的节点
}
ptr.is_end = true; // 最后的节点为单词的最后一个字符 is_end设置为true
}
public boolean search(String word) {
TrieNode ptr = root; // 从根出发
for(int i=0;i<word.length();i++){
char c = word.charAt(i);
if(ptr.child[c-'a'] == null){
return false;
}
ptr = ptr.child[c-'a'];// 移动指针 指向孩子节点
}
return ptr.is_end;
}
public boolean startsWith(String prefix) {
TrieNode ptr = root;
for(int i=0;i<prefix.length();i++){
char c = prefix.charAt(i);
if(ptr.child[c-'a'] == null){
return false;
}
ptr = ptr.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);
*/
// @lc code=end