Skip to content

Commit 88ee419

Browse files
committed
[Function add]
1. Add trie tree results in C++ version.
1 parent 7da7f7c commit 88ee419

4 files changed

+223
-0
lines changed

leetcode/208. Implement Trie (Prefix Tree).md

+59
Original file line numberDiff line numberDiff line change
@@ -280,5 +280,64 @@ class Trie {
280280
*/
281281
```
282282

283+
### C++ Version
284+
```objectc
285+
class Trie {
286+
public:
287+
/** Initialize your data structure here. */
288+
Trie(): root_(new Node()){}
289+
~Trie(){
290+
delete root_;
291+
}
292+
/** Inserts a word into the trie. */
293+
void insert(string word) {
294+
Node* temp = root_;
295+
for(const char c: word){
296+
if(temp->childs[c - 'a'] == nullptr){
297+
temp->childs[c - 'a'] = new Node();
298+
}
299+
temp = temp->childs[c - 'a'];
300+
}
301+
temp->isLeaf = true;
302+
}
303+
/** Returns if the word is in the trie. */
304+
bool search(string word) {
305+
Node* temp = find(std::move(word));
306+
return temp == nullptr ? false: temp->isLeaf;
307+
}
308+
/** Returns if there is any word in the trie that starts with the given prefix. */
309+
bool startsWith(string prefix) {
310+
return find(std::move(prefix)) != nullptr;
311+
}
312+
private:
313+
struct Node{
314+
bool isLeaf;
315+
vector<Node*> childs;
316+
Node(): isLeaf(false), childs(26, nullptr){};
317+
~Node(){
318+
for(auto nodePtr: childs){
319+
delete nodePtr;
320+
}
321+
}
322+
};
323+
Node* find(const string& word){
324+
Node* temp = root_;
325+
for(const char c: word){
326+
if(temp->childs[c - 'a'] == nullptr) return nullptr;
327+
temp = temp->childs[c - 'a'];
328+
}
329+
return temp;
330+
}
331+
Node* root_;
332+
};
333+
/**
334+
* Your Trie object will be instantiated and called as such:
335+
* Trie* obj = new Trie();
336+
* obj->insert(word);
337+
* bool param_2 = obj->search(word);
338+
* bool param_3 = obj->startsWith(prefix);
339+
*/
340+
```
341+
283342
### Reference
284343
1. [Trie Tree 字典树](https://seanforfun.github.io/datastructure/2018/11/07/TrieTree.html)

leetcode/648. Replace Words.md

+55
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,59 @@ class Solution {
8585
return sb.toString().substring(1);
8686
}
8787
}
88+
```
89+
90+
### C++ version
91+
```objectc
92+
class Solution {
93+
public:
94+
string replaceWords(vector<string>& dict, string sentence) {
95+
root_.reset(new Node());
96+
string res = "", temp = "";
97+
for(const string & pattern: dict){
98+
insert(pattern);
99+
}
100+
istringstream is(sentence);
101+
while(is >> temp){
102+
if(!res.empty()) res += " ";
103+
res += findClosest(temp);
104+
}
105+
return res;
106+
}
107+
private:
108+
struct Node{
109+
vector<Node*> childs;
110+
bool isLeaf;
111+
Node(): isLeaf(false), childs(26, nullptr){};
112+
~Node(){
113+
for(auto node: childs){
114+
delete node;
115+
}
116+
}
117+
};
118+
std::unique_ptr<Node> root_;
119+
void insert(const string & word){
120+
Node* temp = root_.get();
121+
for(const char & c: word){
122+
if(!temp->childs[c - 'a']){
123+
temp->childs[c - 'a'] = new Node();
124+
}
125+
temp = temp->childs[c - 'a'];
126+
}
127+
temp->isLeaf = true;
128+
}
129+
string findClosest(const string & word){
130+
Node* temp = root_.get();
131+
string cur = "";
132+
for(const char c: word){
133+
cur += c;
134+
if(temp->childs[c - 'a']){
135+
if(temp->childs[c - 'a']->isLeaf)
136+
return cur;
137+
else temp = temp->childs[c - 'a'];
138+
}else return word;
139+
}
140+
return word;
141+
}
142+
};
88143
```

leetcode/676. Implement Magic Dictionary.md

+49
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,52 @@ class MagicDictionary {
8787
* boolean param_2 = obj.search(word);
8888
*/
8989
```
90+
91+
### C++ version
92+
```objectc
93+
class MagicDictionary {
94+
public:
95+
/** Initialize your data structure here. */
96+
MagicDictionary() {
97+
map_.clear();
98+
}
99+
100+
/** Build a dictionary through a list of words */
101+
void buildDict(vector<string> dict) {
102+
for(string& s: dict){
103+
int len = s.length();
104+
for(int i = 0; i < len; ++i){
105+
char c = s[i];
106+
s[i] = '*';
107+
map_[s].insert(c);
108+
s[i] = c;
109+
}
110+
}
111+
}
112+
113+
/** Returns if there is any word in the trie that equals to the given word after modifying exactly one character */
114+
bool search(string word) {
115+
int len = word.length();
116+
for(int i = 0; i < len; ++i){
117+
char c = word[i];
118+
word[i] = '*';
119+
auto entry = map_.find(word);
120+
word[i] = c;
121+
if(entry == map_.end()) word[i] = c;
122+
else{
123+
if(entry->second.size() > 1 || c != *(entry->second.begin())) return true;
124+
}
125+
}
126+
return false;
127+
}
128+
private:
129+
unordered_map<string, unordered_set<char>> map_;
130+
};
131+
132+
/**
133+
* Your MagicDictionary object will be instantiated and called as such:
134+
* MagicDictionary* obj = new MagicDictionary();
135+
* obj->buildDict(dict);
136+
* bool param_2 = obj->search(word);
137+
*/
138+
```

leetcode/677. Map Sum Pairs.md

+60
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,63 @@ class MapSum {
8181
* int param_2 = obj.sum(prefix);
8282
*/
8383
```
84+
85+
### C++ Version
86+
```objectc
87+
class MapSum {
88+
public:
89+
/** Initialize your data structure here. */
90+
MapSum() {
91+
root_.reset(new Node());
92+
}
93+
void insert(string key, int val) {
94+
Node* temp = root_.get();
95+
Node* test = contains(key);
96+
int diff = (test && test->isLeaf) ? val - test->sum: val;
97+
temp->sum += diff;
98+
for(const char & c: key){
99+
if(!temp->childs[c - 'a']){
100+
temp->childs[c - 'a'] = new Node();
101+
}
102+
temp = temp->childs[c - 'a'];
103+
temp->sum += diff;
104+
}
105+
temp->isLeaf = true;
106+
}
107+
int sum(string prefix) {
108+
Node* temp = root_.get();
109+
for(const char & c: prefix){
110+
if(!temp->childs[c - 'a']) return 0;
111+
temp = temp->childs[c - 'a'];
112+
}
113+
return temp->sum;
114+
}
115+
private:
116+
struct Node{
117+
vector<Node*> childs;
118+
int sum;
119+
bool isLeaf;
120+
Node(): childs(26, nullptr), sum(0), isLeaf(false){};
121+
~Node(){
122+
for(auto node: childs){
123+
delete node;
124+
}
125+
}
126+
};
127+
Node* contains(string word){
128+
Node * temp = root_.get();
129+
for(const char & c: word){
130+
if(!temp->childs[c - 'a']) return nullptr;
131+
temp = temp->childs[c - 'a'];
132+
}
133+
return temp;
134+
}
135+
unique_ptr<Node> root_;
136+
};
137+
/**
138+
* Your MapSum object will be instantiated and called as such:
139+
* MapSum* obj = new MapSum();
140+
* obj->insert(key,val);
141+
* int param_2 = obj->sum(prefix);
142+
*/
143+
```

0 commit comments

Comments
 (0)