Skip to content

Commit f083dd9

Browse files
add 208 js, go and some explain for cpp
1 parent 43fa746 commit f083dd9

File tree

4 files changed

+113
-2
lines changed

4 files changed

+113
-2
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -186,10 +186,11 @@ LeetCode
186186
|0205|[Isomorphic Strings](https://leetcode.com/problems/isomorphic-strings/) | c | [c++](./src/0205-Isomorphic-Strings/0205.cpp) |[python](./src/0205-Isomorphic-Strings/0205.py)|||Easy|
187187
|0206|[Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/) | c | [c++](./src/0206-Reverse-Linked-List/0206.cpp) |[python](./src/0206-Reverse-Linked-List/0206.py)|||Easy|
188188
|0207|[Course Schedule](https://leetcode.com/problems/course-schedule/) | c | [c++](./src/0207-Course-Schedule/0207.cpp) |[python](./src/0207-Course-Schedule/0207.py)|[go](./src/0207-Course-Schedule/0207.go)|[js](./src/0207-Course-Schedule/0207.js)|Medium|
189-
|0208|[Implement Trie (Prefix Tree)](https://leetcode.com/problems/implement-trie-prefix-tree/) | c | [c++](./src/0208-Implement-Trie-(Prefix-Tree)/0208.cpp) |[python](./src/0208-Implement-Trie-(Prefix-Tree)/0208.py)|||Easy|
189+
|0208|[Implement Trie (Prefix Tree)](https://leetcode.com/problems/implement-trie-prefix-tree/) | c | [c++](./src/0208-Implement-Trie-(Prefix-Tree)/0208.cpp) |[python](./src/0208-Implement-Trie-(Prefix-Tree)/0208.py)|||Medium|
190190
|0209|[Minimum Size Subarray Sum](https://leetcode.com/problems/minimum-size-subarray-sum/) | c | [c++](./src/0209-Minimum-Size-Subarray-Sum/0209.cpp) |[python](./src/0209-Minimum-Size-Subarray-Sum/0209.py)|||Medium|
191191
|0210|[Course Schedule II](https://leetcode.com/problems/course-schedule-ii/) | c | [c++](./src/0210-Course-Schedule-II/0210.cpp) |[python](./src/0210-Course-Schedule-II/0210.py)|[go](./src/0210-Course-Schedule-II/0210.go)|[js](./src/0210-Course-Schedule-II/0210.js)|Medium|
192192
|0211|[Add and Search Word - Data structure design](https://leetcode.com/problems/add-and-search-word-data-structure-design/) | c | [c++](./src/0211-Add-and-Search-Word-Data-structure-design/0211.cpp) |[python](./src/0211-Add-and-Search-Word-Data-structure-design/0211.py)|||Medium|
193+
|0212|[Word Search II](https://leetcode.com/problems/word-search-ii/) | c | [c++](./src/0212-Word-Search-II/0212.cpp) |[python](./src/0212-Word-Search-II/0212.py)|[go](./src/0212-Word-Search-II/0212.go)|[js](./src/0212-Word-Search-II/0212.js)|Medium|
193194
|0213|[House Robber II](https://leetcode.com/problems/house-robber-ii/) | c | [c++](./src/0213-House-Robber-II/0213.cpp) |[python](./src/0213-House-Robber-II/0213.py)|||Medium|
194195
|0215|[Kth Largest Element in an Array](https://leetcode.com/problems/kth-largest-element-in-an-array/) | c | [c++](./src/0215-Kth-Largest-Element-in-an-Array/0215.cpp) |[python](./src/0215-Kth-Largest-Element-in-an-Array/0215.py)|||Medium|
195196
|0216|[Combination Sum III](https://leetcode.com/problems/combination-sum-iii/) | c | [c++](./src/0216-Combination-Sum-III/0216.cpp) |[python](./src/0216-Combination-Sum-III/0216.py)|||Medium|

src/0208-Implement-Trie-(Prefix-Tree)/0208.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ class Trie
2626
auto cur = _root;
2727
for (auto c : word)
2828
{
29-
if (!(cur->next).count(c)) cur->next[c] = new Node();
29+
if (!(cur->next).count(c)) // or if (cur->next[c] == nullptr)
30+
cur->next[c] = new Node();
3031
cur = cur->next[c];
3132
}
3233
if (!cur->isWord) cur->isWord = true;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
type Trie struct {
2+
next map[rune]*Trie
3+
isWord bool
4+
}
5+
6+
7+
/** Initialize your data structure here. */
8+
func Constructor() Trie {
9+
return Trie{next: make(map[rune]*Trie)}
10+
}
11+
12+
13+
/** Inserts a word into the trie. */
14+
func (this *Trie) Insert(word string) {
15+
cur := this
16+
for _, c := range word {
17+
if cur.next[c] == nil {
18+
cur.next[c] = &Trie{next: make(map[rune]*Trie)}
19+
}
20+
cur = cur.next[c]
21+
}
22+
if !cur.isWord {
23+
cur.isWord = true
24+
}
25+
}
26+
27+
28+
func (this *Trie) search(word string) *Trie {
29+
cur := this
30+
for _, c := range word {
31+
if cur.next[c] == nil {
32+
return nil
33+
}
34+
cur = cur.next[c]
35+
}
36+
return cur
37+
}
38+
39+
/** Returns if the word is in the trie. */
40+
func (this *Trie) Search(word string) bool {
41+
cur := this.search(word)
42+
if cur != nil && cur.isWord {
43+
return true
44+
}
45+
return false
46+
}
47+
48+
49+
/** Returns if there is any word in the trie that starts with the given prefix. */
50+
func (this *Trie) StartsWith(prefix string) bool {
51+
if this.search(prefix) != nil {
52+
return true
53+
}
54+
return false
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* Initialize your data structure here.
3+
*/
4+
var Trie = function() {
5+
this.next = {};
6+
this.isWord = false;
7+
};
8+
9+
/**
10+
* Inserts a word into the trie.
11+
* @param {string} word
12+
* @return {void}
13+
*/
14+
Trie.prototype.insert = function(word) {
15+
let cur = this;
16+
for (let c of word) {
17+
if (!cur.next[c]) {
18+
cur.next[c] = new Trie();
19+
}
20+
cur = cur.next[c];
21+
}
22+
if (!cur.isWord) cur.isWord = true;
23+
};
24+
25+
26+
Trie.prototype.find = function(word) {
27+
let cur = this;
28+
for (let c of word) {
29+
if (!cur.next[c]) return null;
30+
cur = cur.next[c];
31+
}
32+
return cur;
33+
};
34+
35+
/**
36+
* Returns if the word is in the trie.
37+
* @param {string} word
38+
* @return {boolean}
39+
*/
40+
Trie.prototype.search = function(word) {
41+
let cur = this.find(word);
42+
if (cur != null && cur.isWord) return true;
43+
return false;
44+
};
45+
46+
/**
47+
* Returns if there is any word in the trie that starts with the given prefix.
48+
* @param {string} prefix
49+
* @return {boolean}
50+
*/
51+
Trie.prototype.startsWith = function(prefix) {
52+
if (this.find(prefix) != null) return true;
53+
return false;
54+
};

0 commit comments

Comments
 (0)