Create 72.cpp

This commit is contained in:
Kirigaya Kazuto 2022-05-22 18:42:33 +08:00 committed by GitHub
parent c25e64fbd1
commit 2a420466b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

34
LeetCode-CN/72.cpp Normal file
View File

@ -0,0 +1,34 @@
class Solution {
public:
int dp[501][501];
int minDistance(string word1, string word2) {
memset(dp, 0, sizeof(dp));
for(int i=1; i<=word1.size(); i++) {
dp[i][0] = i;
}
for(int i=1; i<=word2.size(); i++) {
dp[0][i] = i;
}
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] = 1 + min(min(dp[i-1][j], dp[i][j-1]), dp[i-1][j-1]);
}
}
}
/*
for(int i=0; i<= word1.size(); i++) {
for(int j=0; j<=word2.size(); j++) {
printf("%d ", dp[i][j]);
}
printf("\n");
}
*/
return dp[word1.size()][word2.size()];
}
};