0072. 编辑距离 #50
utterances-bot
started this conversation in
Comments
Replies: 0 comments 1 reply
-
附C++代码明确 补充说明下,本题
const int N = 5e2+10;
class Solution {
public:
int minDistance(string word1, string word2) {
int dp[N][N] = {0};
int leni = word1.size();
int lenj = word2.size();
for(int i = 1; i <= leni; ++i) dp[i][0] = i;
for(int j = 1; j <= lenj; ++j) dp[0][j] = j;
for(int i = 1; i <= leni; ++i) {
for(int j = 1; j <= lenj; ++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-1][j], dp[i][j-1]))+1;
}
}
return dp[leni][lenj];
}
}; |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
0072. 编辑距离 | 算法通关手册
https://algo.itcharge.cn/Solutions/0001-0099/edit-distance/
Beta Was this translation helpful? Give feedback.
All reactions