Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 30bbd3f

Browse files
author
bohrzhang
committed
2020年05月19日
1 parent cdf6822 commit 30bbd3f

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

‎208.实现Trie.md‎

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
### 描述:
2+
实现一个 Trie (前缀树),包含 insert, search, 和 startsWith 这三个操作。
3+
4+
### 示例:
5+
```
6+
Trie trie = new Trie();
7+
8+
trie.insert("apple");
9+
trie.search("apple"); // 返回 true
10+
trie.search("app"); // 返回 false
11+
trie.startsWith("app"); // 返回 true
12+
trie.insert("app");
13+
trie.search("app"); // 返回 true
14+
```
15+
16+
### 解析:
17+
定义TrieNode构造函数,其中nodes为该节点能通向的下一个字符节点,标注每个节点是否为结束节点。Trie构造函数中初始化root,insert构建整个trie树,search根据深度遍历来查找匹配路径。
18+
19+
单操作时间复杂度: O(n)
20+
21+
```javascript
22+
var TrieNode = function() {
23+
this.nodes = {}
24+
this.isEnd = false
25+
26+
this.setNode = function(char, node) {
27+
if(this.nodes[char]) return this.nodes[char]
28+
else {
29+
this.nodes[char] = node
30+
return node
31+
}
32+
}
33+
34+
this.findNode = function(char) {
35+
return this.nodes[char]
36+
}
37+
38+
this.setEnd = function() {
39+
this.isEnd = true
40+
}
41+
}
42+
43+
/**
44+
* Initialize your data structure here.
45+
*/
46+
var Trie = function() {
47+
this.root = new TrieNode()
48+
};
49+
50+
/**
51+
* Inserts a word into the trie.
52+
* @param {string} word
53+
* @return {void}
54+
*/
55+
Trie.prototype.insert = function(word) {
56+
let cursor = this.root
57+
for(let i=0; i<word.length; i++) {
58+
const temp = new TrieNode()
59+
const node = cursor.setNode(word[i], temp)
60+
cursor = node
61+
}
62+
cursor.setEnd()
63+
};
64+
65+
/**
66+
* Returns if the word is in the trie.
67+
* @param {string} word
68+
* @return {boolean}
69+
*/
70+
Trie.prototype.search = function(word) {
71+
let cursor = this.root
72+
for(let i=0; i<word.length; i++) {
73+
const node = cursor.findNode(word[i])
74+
if(!node) return false
75+
cursor = node
76+
}
77+
return cursor.isEnd
78+
};
79+
80+
/**
81+
* Returns if there is any word in the trie that starts with the given prefix.
82+
* @param {string} prefix
83+
* @return {boolean}
84+
*/
85+
Trie.prototype.startsWith = function(prefix) {
86+
let cursor = this.root
87+
for(let i=0; i<prefix.length; i++) {
88+
const node = cursor.findNode(prefix[i])
89+
if(!node) return false
90+
cursor = node
91+
}
92+
return true
93+
};
94+
```

‎README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
[131. 分割回文串(困难)](./131.分割回文串.md)
1212

13+
[208. 实现Trie(中等)](./208.实现Trie.md)
14+
1315
[315. 计算右侧小于当前元素的个数(困难)](./315.计算右侧小于当前元素的个数.md)
1416

1517
[329. 矩阵中的最长递增路径(困难)](./329.矩阵中的最长递增路径.md)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /