Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions Week_05/G20190343020268/0268_Week 05(cpp)
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//72. 编辑距离
class Solution {
public:
int minDistance(string word1, string word2) {
int m = word1.size();
int n = word2.size();
if(m*n==0)return m+n;
vector<vector<int>> dp(m+1,vector<int>(n+1));
for(int i=0;i<m+1;i++)
dp[i][0] = i;
for(int j = 0;j<n+1;j++)
dp[0][j] = j;
for(int i=1;i<m+1;i++){
for(int j=1;j<n+1;j++){
if(word1[i-1]==word2[j-1])
dp[i][j] = min(min(dp[i-1][j]+1,dp[i][j-1]+1),dp[i-1][j-1]);
else
dp[i][j] = min(min(dp[i-1][j],dp[i][j-1]),dp[i-1][j-1])+1;

}
}
return dp[m][n];
}

}