Skip to content

Commit 480b75d

Browse files
committed
week07
1 parent 66a72f1 commit 480b75d

1 file changed

Lines changed: 224 additions & 0 deletions

File tree

Week07/week07作业.md

Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
![image-20200724101337721](C:\Users\RL\AppData\Roaming\Typora\typora-user-images\image-20200724101337721.png)
2+
3+
#### Trie树理论
4+
5+
![image-20200724101620805](C:\Users\RL\AppData\Roaming\Typora\typora-user-images\image-20200724101620805.png)
6+
7+
![image-20200724101740662](C:\Users\RL\AppData\Roaming\Typora\typora-user-images\image-20200724101740662.png)
8+
9+
![image-20200724101959980](C:\Users\RL\AppData\Roaming\Typora\typora-user-images\image-20200724101959980.png)
10+
11+
![image-20200724102059615](C:\Users\RL\AppData\Roaming\Typora\typora-user-images\image-20200724102059615.png)
12+
13+
14+
15+
Trie 树优于哈希表的另一个理由是,随着哈希表大小增加,会出现大量的冲突,时间复杂度可能增加到 O(n),其中 n是插入的键的数量。与哈希表相比,Trie 树在存储多个具有相同前缀的键时可以使用较少的空间。此时 Trie 树只需要 O(m)的时间复杂度,其中 m 为键长。而在平衡树中查找键值需要 O(mlogn) 时间复杂度。
16+
17+
应用:
18+
19+
1. 自动补全,eg: 谷歌的搜索建议
20+
21+
2. 拼写检查, eg: 文字处理软件中的拼写检查
22+
23+
3. IP 路由 (最长前缀匹配), eg: 使用Trie树的最长前缀匹配算法,Internet 协议(IP)路由中利用转发表选择路径。
24+
25+
4. T9 (九宫格) 打字预测, eg: T9(九宫格输入),在 20 世纪 90 年代常用于手机输入
26+
27+
5. 单词游戏, eg: Trie 树可通过剪枝搜索空间
28+
29+
30+
31+
```
32+
208. 实现 Trie (前缀树)
33+
实现一个 Trie (前缀树),包含 insert, search, 和 startsWith 这三个操作。
34+
35+
示例:
36+
37+
Trie trie = new Trie();
38+
39+
trie.insert("apple");
40+
trie.search("apple"); // 返回 true
41+
trie.search("app"); // 返回 false
42+
trie.startsWith("app"); // 返回 true
43+
trie.insert("app");
44+
trie.search("app"); // 返回 true
45+
说明:
46+
47+
你可以假设所有的输入都是由小写字母 a-z 构成的。
48+
保证所有输入均为非空字符串。
49+
```
50+
51+
52+
53+
```python
54+
class Trie:
55+
56+
def __init__(self):
57+
"""
58+
Initialize your data structure here.
59+
"""
60+
self.root = {}
61+
self.end_of_word = "#"
62+
63+
def insert(self, word: str) -> None:
64+
"""
65+
Inserts a word into the trie.
66+
"""
67+
node = self.root
68+
for char in word:
69+
node = node.setdefault(char, {})
70+
node[self.end_of_word] = self.end_of_word
71+
72+
def search(self, word: str) -> bool:
73+
"""
74+
Returns if the word is in the trie.
75+
"""
76+
node = self.root
77+
for char in word:
78+
if char not in node:
79+
return False
80+
node = node[char]
81+
return self.end_of_word in node
82+
83+
def startsWith(self, prefix: str) -> bool:
84+
"""
85+
Returns if there is any word in the trie that starts with the given prefix.
86+
"""
87+
node = self.root
88+
for char in prefix:
89+
if char not in node:
90+
return False
91+
node = node[char]
92+
return True
93+
94+
95+
96+
# Your Trie object will be instantiated and called as such:
97+
# obj = Trie()
98+
# obj.insert(word)
99+
# param_2 = obj.search(word)
100+
# param_3 = obj.startsWith(prefix)
101+
```
102+
103+
104+
105+
单词搜索2
106+
107+
![image-20200725081657635](C:\Users\RL\AppData\Roaming\Typora\typora-user-images\image-20200725081657635.png)
108+
109+
![image-20200725081819375](C:\Users\RL\AppData\Roaming\Typora\typora-user-images\image-20200725081819375.png)
110+
111+
112+
113+
114+
115+
![image-20200725101845954](C:\Users\RL\AppData\Roaming\Typora\typora-user-images\image-20200725101845954.png)
116+
117+
118+
119+
#### 并查集
120+
121+
![image-20200726190851020](C:\Users\RL\AppData\Roaming\Typora\typora-user-images\image-20200726190851020.png)
122+
123+
![image-20200725101230990](C:\Users\RL\AppData\Roaming\Typora\typora-user-images\image-20200725101230990.png<img src="C:\Users\RL\AppData\Roaming\Typora\typora-user-images\image-20200725101211828.png" style="zoom:33%;" />
124+
125+
126+
127+
#### [547. 朋友圈](https://leetcode-cn.com/problems/friend-circles/)[中等]
128+
129+
[1 [0726]]
130+
131+
```
132+
547. 朋友圈
133+
班上有 N 名学生。其中有些人是朋友,有些则不是。他们的友谊具有是传递性。如果已知 A 是 B 的朋友,B 是 C 的朋友,那么我们可以认为 A 也是 C 的朋友。所谓的朋友圈,是指所有朋友的集合。
134+
135+
给定一个 N * N 的矩阵 M,表示班级中学生之间的朋友关系。如果M[i][j] = 1,表示已知第 i 个和 j 个学生互为朋友关系,否则为不知道。你必须输出所有学生中的已知的朋友圈总数。
136+
137+
示例 1:
138+
139+
输入:
140+
[[1,1,0],
141+
[1,1,0],
142+
[0,0,1]]
143+
输出: 2
144+
说明:已知学生0和学生1互为朋友,他们在一个朋友圈。
145+
第2个学生自己在一个朋友圈。所以返回2。
146+
示例 2:
147+
148+
输入:
149+
[[1,1,0],
150+
[1,1,1],
151+
[0,1,1]]
152+
输出: 1
153+
说明:已知学生0和学生1互为朋友,学生1和学生2互为朋友,所以学生0和学生2也是朋友,所以他们三个在一个朋友圈,返回1。
154+
注意:
155+
156+
N 在[1,200]的范围内。
157+
对于所有学生,有M[i][i] = 1。
158+
如果有M[i][j] = 1,则有M[j][i] = 1。
159+
```
160+
161+
162+
163+
```python
164+
class Solution:
165+
def findCircleNum(self, M: List[List[int]]) -> int:
166+
if not M: return 0
167+
168+
n = len(M)
169+
p = [i for i in range(n)]
170+
171+
for i in range(n):
172+
for j in range(n):
173+
if M[i][j] == 1:
174+
# 遍历矩阵,合并i,j
175+
self._union(p, i, j)
176+
177+
return len(set([self._parent(p, i) for i in range(n)]))
178+
179+
def _union(self, p, i, j):
180+
p1 = self._parent(p, i)
181+
p2 = self._parent(p, j)
182+
p[p2] = p1
183+
184+
def _parent(self, p, i):
185+
root = i
186+
while p[root] != root:
187+
root = p[root]
188+
while p[i] != i:
189+
x = i; i = p[i]; p[x] = root
190+
return root
191+
192+
```
193+
194+
195+
196+
![image-20200725103138542](C:\Users\RL\AppData\Roaming\Typora\typora-user-images\image-20200725103138542.png)
197+
198+
199+
200+
201+
202+
![image-20200725104510761](C:\Users\RL\AppData\Roaming\Typora\typora-user-images\image-20200725104510761.png)
203+
204+
205+
206+
207+
208+
八皇后
209+
210+
![image-20200725104939225](C:\Users\RL\AppData\Roaming\Typora\typora-user-images\image-20200725104939225.png)
211+
212+
213+
214+
215+
216+
217+
218+
![image-20200725142227456](C:\Users\RL\AppData\Roaming\Typora\typora-user-images\image-20200725142227456.png)
219+
220+
AVL 和红黑树
221+
222+
![image-20200726201312875](C:\Users\RL\AppData\Roaming\Typora\typora-user-images\image-20200726201312875.png)
223+
224+
四种旋转,和为什么引入旋转

0 commit comments

Comments
 (0)