LeetCode 72. Edit Distance
2016-12-02 15:32
423 查看
描述
问一个字符串需要多少的操作才能变成另一个字符串解决
动态规划class Solution { public: int minDistance(string word1, string word2) { int length1 = word1.size(); int length2 = word2.size(); vector<vector<int>> dp(length1 + 1, vector<int>(length2 + 1, 0)); for (int i = 0; i <= length1; ++i) dp[i][0] = i; for (int j = 0; j <= length2; ++j) dp[0][j] = j; for (int i = 0; i < length1; ++i) { for (int j = 0; j < length2; ++j) { int cost = word1[i] == word2[j] ? 0 : 1; dp[i + 1][j + 1] = min(min(dp[i][j + 1] + 1, dp[i + 1][j] + 1), dp[i][j] + cost); } } return dp[length1][length2]; } };
相关文章推荐
- LeetCode 72. Edit Distance
- [leetcode] 72. Edit Distance 解题报告
- Leetcode 72. Edit Distance
- 72. Edit Distance , LeetCode
- 第九周 leetcode 72. Edit Distance(Hard)
- leetcode-72. Edit Distance
- LeetCode 72. Edit Distance-----Python实现
- Leetcode:72. Edit Distance
- [LeetCode] 72. Edit Distance 编辑距离
- LeetCode-72. Edit Distance (JAVA)字符串最小编辑距离DP&DFS
- leetcode 72. Edit Distance
- Leetcode 72. Edit Distance
- [leetcode] 72. Edit Distance
- leetcode 72. Edit Distance
- LeetCode dynamic programming 72. Edit Distance
- 【leetcode】72. Edit Distance【java】
- leetcode - 72. Edit Distance和 583. Delete Operation for Two Strings
- 【LeetCode】72. Edit Distance
- leetcode 72. Edit Distance
- 第十周:[leetCode] 72. Edit Distance