代码随想录day45 115.不同的子序列 583. 两个字符串的删除操作 72. 编辑距离
代码随想录
class Solution {
public:
int numDistinct(string s, string t) {
int size1 = s.size();
int size2 = t.size();
vector<vector<uint64_t>> dp(size1 + 1, vector<uint64_t>(size2 + 1, 0));
for (int i = 0; i < size1; i++) {
dp[i][0] = 1;
}
for (int i = 1; i <= size1; i++) {
for (int j = 1; j <= size2; 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[size1][size2];
}
};
代码随想录
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
int minDistance(string word1, string word2) {
int size1 = word1.size();
int size2 = word2.size();
vector<vector<int>> dp(size1 + 1, vector<int>(size2 + 1, 0));
int len = 0;
for (int i = 1; i <= size1; i++) {
for (int j = 1; j <= size2; j++) {
if (word1[i - 1] == word2[j - 1]) {
dp[i][j] = dp[i - 1][j - 1] + 1;
} else {
dp[i][j] = max(max(dp[i - 1][j], dp[i][j - 1]), dp[i - 1][j - 1]);
}
len = len < dp[i][j] ? dp[i][j] : len;
}
}
return size1 + size2 - 2 * len;
}
};
class Solution {
public:
int minDistance(string word1, string word2) {
int size1 = word1.size();
int size2 = word2.size();
vector<vector<int>> dp(size1 + 1, vector<int>(size2 + 1, 0));
for (int i = 0; i <= size1; i++) {
dp[i][0] = i;
}
for (int j = 0; j <= size2; j++) {
dp[0][j] = j;
}
for (int i = 1; i <= size1; i++) {
for (int j = 1; j <= size2; 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, dp[i][j - 1] + 1);
}
}
}
return dp[size1][size2];
}
};
代码随想录
class Solution {
public:
int minDistance(string word1, string word2) {
int size1 = word1.size();
int size2 = word2.size();
vector<vector<int>> dp(size1 + 1, vector<int>(size2 + 1));
for (int i = 0; i <= size1; i++) {
dp[i][0] = i;
}
for (int j = 1; j <= size2; j++) {
dp[0][j] = j;
}
for (int i = 1; i <= size1; i++) {
for (int j = 1; j <= size2; j++) {
if (word1[i - 1] == word2[j - 1]) {
dp[i][j] = dp[i - 1][j - 1];
} else {
dp[i][j] = min(min(dp[i - 1][j] + 1, dp[i][j - 1] + 1), dp[i - 1][j - 1] + 1);
}
}
}
return dp[size1][size2];
}
};