mirror of
https://github.com/Kiritow/OJ-Problems-Source.git
synced 2024-03-22 13:11:29 +08:00
Create 72.cpp
This commit is contained in:
parent
c25e64fbd1
commit
2a420466b6
34
LeetCode-CN/72.cpp
Normal file
34
LeetCode-CN/72.cpp
Normal 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()];
|
||||
}
|
||||
};
|
Loading…
Reference in New Issue
Block a user