leetcode 72. Edit Distance
来源:互联网 发布:瑞士军刀淘宝能卖吗 编辑:程序博客网 时间:2024/06/02 06:59
leetcode 72. Edit Distance 解题报告
描述
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.)
You have the following 3 operations permitted on a word:
a) Insert a character
b) Delete a character
c) Replace a character
分析
题目要求找到2个字符串之间的最小距离
定义执行特定变换可以转换为另一个字串的次数:
操作有
1.插入
2.删除
3.替换
最简单的思路就是先比较长度,计算出需要插入的次数或者删除的次数,
然后按照目标串的字符排列字符排列遍历得到需要替换的次数这个操作要每次都遍历一次目标串,复杂度为O(n^2)
现在使用动态规划的思想考虑
使用矩阵dp[i][j]来表示str1的前i项变换到str2的前j项需要的最小操作次数.
可以得到下面的递推公式:
if (str1[i] == str2[j]) // 当最后一个字符相同的时候 // 那么变换的最小值就是两个字符都去掉最后一个的情况 dp[i][j] = dp[i-1][j-1];else // 如果最后一个字符不相同 // 那么变换的最小值就是str1去掉最后一个字符在加一次删除操作 /* 例如 str1 str2 需要操作的次数 123 ---> 2345 = dp[i][j] 12 ----> 2345 = dp[i-1][j] 由于3!=5 那么可以知道 123-->12需要一次删除操作, 那么dp[i][j]通过123变为2345就要 先从123-->12在12---->2345, 总共需要dp[i-1][j]+1次操作 */ // 或者str2去掉一个字符再加一次插入操作 // 或者str1和str2都去掉最后一个字符外加一次替换操作 // 最终答案为3者的最小值 dp[i][j] = min(min(dp[i-1][j], dp[i][j-1]), dp[i-1][j-1]) + 1;
AC代码
class Solution {public: int minDistance(string word1, string word2) { int dp[word1.length()+1][word2.length()+1] = {{0}}; for (int i = 0; i <= word1.length(); ++i) { dp[i][0] = i; } for (int i = 0; i <= word2.length(); ++i) { dp[0][i] = i; } for (int i = 1; i <= word1.length(); ++i) { for (int j = 1; j <= word2.length(); ++j) { if (word1[i-1] == word2[j-1]) { dp[i][j] = 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[word1.length()][word2.length()]; }};
阅读全文
0 0
- LeetCode 72. Edit Distance
- [LeetCode]72.Edit Distance
- LeetCode --- 72. Edit Distance
- [Leetcode] 72. Edit Distance
- [leetcode] 72.Edit Distance
- [leetcode] 72.Edit Distance
- Leetcode 72. Edit Distance
- LeetCode 72. Edit Distance
- leetcode 72. Edit Distance
- LeetCode 72. Edit Distance
- Leetcode 72. Edit Distance
- Leetcode:72. Edit Distance
- 【LeetCode】72. Edit Distance
- leetCode 72. Edit Distance
- [leetcode] 72. Edit Distance
- [LeetCode] 72. Edit Distance
- leetcode 72. Edit Distance
- leetcode 72. Edit Distance
- Linux基本命令---1
- 【知识整理】这可能是最好的性能优化教程(一)
- hdu-1045-Fire Net-二分图匹配-匈牙利算法-java
- int和Integer的区别 String和StringBuffer的区别
- ResNet 《Deep Residual Learning for Image Recognition》 阅读笔记
- leetcode 72. Edit Distance
- core-site.xml
- Java获得范型类型class
- JAVA软件设计思想之我见之架构设计(3)
- 设置事务模式及隔离级别
- org.apache.catalina.util.DefaultAnnotationProcessor cannot be cast to org.ap解决方案
- http://www.cnblogs.com/liangcheng11/p/6380922.html
- mybatis_study02
- chrome插件系列一:Secure Shell(替代ssh客户端)