Skip to content

Commit fce5aa6

Browse files
committed
update
1 parent 2df9595 commit fce5aa6

4 files changed

+112
-0
lines changed

47. 编辑距离.md

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
***编辑距离指的就是,将一个字符串转化成另一个字符串,需要的最少编辑操作次数(比如增加一个字符、删除一个字符、替换一个字符)。编辑距离越大,说明两个字符串的相似程度越小;相反,编辑距离就越小,说明两个字符串的相似程度越大。对于两个完全相同的字符串来说,编辑距离就是 0。***
2+
3+
```
4+
def edit_dist(s,t):
5+
#假设字符串 s 和 t 的长度分别为 m 和 n,创建 m+1 行 n+1 列的二维数组 dp,其中 dp[i][j] 表示 s[0:i] 和 t[0:j]的编辑距离。
6+
m = len(s)
7+
n = len(t)
8+
dp = [[0]*(n+1) for i in range(m+1)]
9+
#填充第一行
10+
for j in range(n+1):
11+
dp[0][j] = j
12+
#填充第一列
13+
for i in range(m+1):
14+
dp[i][0] = i
15+
16+
for i in range(1, m+1):
17+
for j in range(1, n+1):
18+
if s[i-1] == t[j-1]:
19+
dp[i][j] = dp[i-1][j-1]
20+
else:
21+
dp[i][j] = min(dp[i-1][j], dp[i][j-1], dp[i-1][j-1])+1
22+
return dp[-1][-1]
23+
```

48. 最长公共子序列.md

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
***求两字符串的最长公共子序列***
2+
3+
```
4+
def longestCommonSubsequence(s, t):
5+
#假设字符串 s 和 t 的长度分别为 m 和 n,创建 m+1 行 n+1 列的二维数组 dp,其中 dp[i][j] 表示 s[0:i] 和 t[0:j]的最长公共子序列的长度。
6+
m = len(s)
7+
n = len(t)
8+
dp = [[0]*(n+1) for _ in range(m+1)]
9+
10+
for i in range(1, m+1):
11+
for j in range(1, n+1):
12+
if s[i-1] == t[j-1]:
13+
dp[i][j] = dp[i-1][j-1]+1
14+
else:
15+
dp[i][j] = max(dp[i-1][j], dp[i][j-1])
16+
return dp[-1][-1]
17+
```

49. 最长公共子串.md

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
***求两字符串的最长公共子串的长度***
2+
3+
```
4+
def LongestCommonSubstring(s, t):
5+
#假设字符串 s 和 t 的长度分别为 m 和 n,创建 m+1 行 n+1 列的二维数组 dp,其中 dp[i][j] 表示 s[0:i] 和 t[0:j]的最大公共后缀子数组的长度。
6+
m = len(s)
7+
n = len(t)
8+
max_len = 0
9+
10+
dp = [[0]*(n+1) for _ in range(m+1)]
11+
for i in range(1, m+1):
12+
for j in range(1, n+1):
13+
if s[i-1] == t[j-1]:
14+
dp[i][j] = dp[i-1][j-1]+1
15+
max_len = max(max_len, dp[i][j])
16+
return max_len
17+
```
18+

50. 实现 Trie (前缀树).md

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
***Trie(发音类似 "try")或者说 前缀树 是一种树形数据结构,用于高效地存储和检索字符串数据集中的键。这一数据结构有相当多的应用情景,例如自动补完和拼写检查。***
2+
3+
```
4+
输入
5+
["Trie", "insert", "search", "search", "startsWith", "insert", "search"]
6+
[[], ["apple"], ["apple"], ["app"], ["app"], ["app"], ["app"]]
7+
输出
8+
[null, null, true, false, true, null, true]
9+
10+
解释
11+
Trie trie = new Trie();
12+
trie.insert("apple");
13+
trie.search("apple"); // 返回 True
14+
trie.search("app"); // 返回 False
15+
trie.startsWith("app"); // 返回 True
16+
trie.insert("app");
17+
trie.search("app"); // 返回 True
18+
```
19+
20+
```
21+
class TrieNode():
22+
def __init__(self, val=None):
23+
self.val = val
24+
self.isEnd = False
25+
self.children = {}
26+
27+
class Trie:
28+
def __init__(self):
29+
self.root = TrieNode()
30+
31+
def insert(self, word: str) -> None:
32+
cur_node = self.root
33+
for c in word:
34+
if c not in cur_node.children:
35+
cur_node.children[c] = TrieNode(c)
36+
cur_node = cur_node.children[c]
37+
cur_node.isEnd = True
38+
39+
def search(self, word: str) -> bool:
40+
cur_node = self.root
41+
for c in word:
42+
if c not in cur_node.children:
43+
return False
44+
cur_node = cur_node.children[c]
45+
return cur_node.isEnd
46+
47+
def startsWith(self, prefix: str) -> bool:
48+
cur_node = self.root
49+
for c in prefix:
50+
if c not in cur_node.children:
51+
return False
52+
cur_node = cur_node.children[c]
53+
return True
54+
```

0 commit comments

Comments
 (0)