碎碎念:还债ing
参考:代码随想录
115.不同的子序列
题目链接
思想
动态规划五部曲:
- 确定dp数组以及下标的含义:dp[i][j],以i-1为结尾的s中有以j-1为结尾的t的个数为dp[i][j]。最后结果在dp[s.size][t.size]
- 确定递推公式:如果s[i-1]==t[j-1],dp[i][j] = dp[i-1][j-1]+dp[i-1][j],else dp[i][j]=dp[i-1][j]
- dp数组的初始化:dp[i][0]=1 dp[0][j]=0 dp[0][0]=1
- 确定遍历顺序:两层for循环,遍历s和t,注意遍历的范围
- 打印dp数组:主要用来debug。
题解
// cpp
class Solution {
public:
int numDistinct(string s, string t) {
vector<vector<uint64_t>> dp(s.size() + 1, vector<uint64_t>(t.size() + 1));
for (int i = 0; i < s.size(); i++) dp[i][0] = 1;
for (int j = 1; j < t.size(); j++) dp[0][j] = 0;
for (int i = 1; i <= s.size(); i++) {
for (int j = 1; j <= t.size(); j++) {
if (s[i - 1] == t[j - 1]) {
dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j];
} else {
dp[i][j] = dp[i - 1][j];
}
}
}
return dp[s.size()][t.size()];
}
};
# python
class Solution:
def numDistinct(self, s: str, t: str) -> int:
dp = [[0] * (len(t)+1) for _ in range((len(s)+1))]
for i in range(len(s)):
dp[i][0] = 1
for j in range(1, len(t)):
dp[0][j] = 0
for i in range(1, len(s) + 1):
for j in range(1, len(t) + 1):
if s[i - 1] == t[j - 1]:
dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j]
else:
dp[i][j] = dp[i - 1][j]
return dp[-1][-1]
反思
注意遍历的范围。
583. 两个字符串的删除操作
题目链接
思想
本题和上一题的区别在于,本题的两个字符串都可以做删除操作。
动态规划五部曲:
- 确定dp数组以及下标的含义:dp[i][j],以i-1为结尾的word1,以j-1为结尾的word2,为了让这两个数组相同的最小操作次数为dp[i][j]。最后结果在dp[word1.size][word2.size]
- 确定递推公式:如果word1[i-1]==word2[j-1],dp[i][j] = dp[i-1][j-1],else dp[i][j]=min(dp[i-1][j]+1,dp[i][j-1]+1,dp[i-1][j-1]+2)
当word1[i - 1] 与 word2[j - 1]相同的时候,dp[i][j] = dp[i - 1][j - 1];
当word1[i - 1] 与 word2[j - 1]不相同的时候,有三种情况:
情况一:删word1[i - 1],最少操作次数为dp[i - 1][j] + 1
情况二:删word2[j - 1],最少操作次数为dp[i][j - 1] + 1
情况三:同时删word1[i - 1]和word2[j - 1],操作的最少次数为dp[i - 1][j - 1] + 2
那最后当然是取最小值,所以当word1[i - 1] 与 word2[j - 1]不相同的时候,递推公式:dp[i][j] = min({dp[i - 1][j - 1] + 2, dp[i - 1][j] + 1, dp[i][j - 1] + 1}); - dp数组的初始化:dp[i][0]=i dp[0][j]=j
- 确定遍历顺序:两层for循环,遍历word1和word2,注意遍历的范围
- 打印dp数组:主要用来debug。
题解
// cpp
class Solution {
public:
int minDistance(string word1, string word2) {
vector<vector<int>> dp(word1.size() + 1, vector<int>(word2.size() + 1, 0));
for (int i = 0; i <= word1.size(); i++) dp[i][0] = i;
for (int j = 1; j <= word2.size(); j++) dp[0][j] = j;
for (int i = 1; i <= word1.size(); i++) {
for (int j = 1; j <= word2.size(); j++) {
if (word1[i - 1] == word2[j - 1]) {
dp[i][j] = dp[i - 1][j - 1];
} else {
dp[i][j] = min(dp[i - 1][j] + 1, min(dp[i][j - 1] + 1, dp[i - 1][j - 1] + 2));
}
}
}
return dp[word1.size()][word2.size()];
}
};
# python
class Solution:
def minDistance(self, word1: str, word2: str) -> int:
dp = [[0] * (len(word2) + 1) for _ in range(len(word1) + 1)]
for i in range(len(word1) + 1):
dp[i][0] = i
for j in range(len(word2) + 1):
dp[0][j] = j
for i in range(1, len(word1) + 1):
for j in range(1, len(word2) + 1):
if word1[i - 1] == word2[j - 1]:
dp[i][j] = dp[i - 1][j - 1]
else:
dp[i][j] = min(dp[i - 1][j] + 1, dp[i][j - 1] + 1, dp[i - 1][j - 1] + 2)
return dp[-1][-1]
反思
72. 编辑距离
题目链接
思想
和上一题的差别在于,除了删除以外,还可以做增加和替换操作。
动态规划五部曲:
- 确定dp数组以及下标的含义:dp[i][j],以i-1为结尾的word1,以j-1为结尾的word2,为了让这两个数组相同的最小操作次数为dp[i][j]。最后结果在dp[word1.size][word2.size]
- 确定递推公式:如果word1[i-1]==word2[j-1],dp[i][j] = dp[i-1][j-1],else dp[i][j]=min(dp[i-1][j]+1,dp[i][j-1]+1,dp[i-1][j-1]+1) 后面这几部分分别代表删除操作、增加操作(另一种意义上的删除)、替换操作
- dp数组的初始化:dp[i][0]=i dp[0][j]=j
- 确定遍历顺序:两层for循环,遍历word1和word2,注意遍历的范围
- 打印dp数组:主要用来debug。
题解
// cpp
class Solution {
public:
int minDistance(string word1, string word2) {
vector<vector<int>> dp(word1.size() + 1, vector<int>(word2.size() + 1, 0));
for (int i = 0; i <= word1.size(); i++) dp[i][0] = i;
for (int j = 1; j <= word2.size(); j++) dp[0][j] = j;
for (int i = 1; i <= word1.size(); i++) {
for (int j = 1; j <= word2.size(); j++) {
if (word1[i - 1] == word2[j - 1]) {
dp[i][j] = dp[i - 1][j - 1];
} else {
dp[i][j] = min(dp[i - 1][j] + 1, min(dp[i][j - 1] + 1, dp[i - 1][j - 1] + 1));
// 分别代表删除 增加 替换
}
}
}
return dp[word1.size()][word2.size()];
}
};
# python
class Solution:
def minDistance(self, word1: str, word2: str) -> int:
dp = [[0] * (len(word2) + 1) for _ in range(len(word1) + 1)]
for i in range(len(word1) + 1):
dp[i][0] = i
for j in range(len(word2) + 1):
dp[0][j] = j
for i in range(1, len(word1) + 1):
for j in range(1, len(word2) + 1):
if word1[i - 1] == word2[j - 1]:
dp[i][j] = dp[i - 1][j - 1]
else:
dp[i][j] = min(dp[i - 1][j] + 1, dp[i][j - 1] + 1, dp[i - 1][j - 1] + 1)
return dp[-1][-1]
反思
注意和上一题对比学习。