leetcode编辑距离

leetcode编辑距离,第1张

leetcode编辑距离

leetcode题目链接

博客讲解链接
b站视频讲解得很清晰

leetcode C++通过代码:

class Solution {
public:
    int minDistance(string word1, string word2) {
        int m = word1.length();
        int n = word2.length();
        int dp[505][505] = {0};
        for(int j = 0; j <= n; j++)
            dp[0][j] = j;
        for(int i = 0; i <= m; i++)
            dp[i][0] = i;
        for(int i = 1; i <= m; i++)
            for(int j = 1; j <= n; j++){
                int d;
                if(word1[i - 1] == word2[j - 1])
                    d = 0;
                else
                    d = 1;
                
                dp[i][j] = min(min(dp[i - 1][j] + 1, dp[i][j - 1] + 1), dp[i - 1][j - 1] + d);
            }
        return dp[m][n];
    }
};

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5702865.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存