-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path473.h
64 lines (61 loc) · 1.71 KB
/
473.h
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
struct TrieNode{
bool endword;
TrieNode *node[26];
TrieNode(bool end = false) : endword(end){
for(int i = 0; i < 26; i++){
node[i] = nullptr;
}
}
};
class WordDictionary {
public:
WordDictionary(){
root = new TrieNode;
}
/*
* @param word: Adds a word into the data structure.
* @return: nothing
*/
void addWord(string &word) {
// write your code here
TrieNode *dummy = root;
for(int i = 0; i < word.size(); i++){
int j = word[i] - 'a';
if(dummy->node[j] == nullptr){
dummy->node[j] = new TrieNode;
}
dummy = dummy->node[j];
}
dummy->endword = true;
}
/*
* @param word: A word could contain the dot character '.' to represent any one letter.
* @return: if the word is in the data structure.
*/
bool search(string &word) { // 递归寻找
// write your code here
return search(word, root, 0, word.length());
}
bool search(string &word, TrieNode *root, int pos, int length){
if(root == nullptr) return false;
if(pos == length) return root->endword;
if(word[pos] == '.'){
for(int i = 0; i < 26; i++){
if(root->node[i] != nullptr){
if(search(word, root->node[i], pos+1, length)){
return true;
}
}
}
}
else{ // != '.'
int j = word[pos] - 'a';
if(root->node[j] != nullptr){
return search(word, root->node[j], pos+1, length);
}
}
return false;
}
private:
TrieNode *root;
};