leetcode 72. Edit Distance
来源:互联网 发布:网络传播推广方案 编辑:程序博客网 时间:2024/05/18 02:33
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
这种题就是动态规划来做!f[i][j]代表word1的前i位变成word2的前j位需要多少步骤。
class Solution {public: int minDistance(string word1, string word2) { int row = word1.size(); int col = word2.size(); int f[row + 1][col + 1] = {0}; for (int i = 0; i <= row; i++) { for (int j = 0; j <= col; j++) { if (i == 0) f[i][j] = j; else if (j == 0) f[i][j] = i; else { if (word1[i - 1] == word2[j - 1]) f[i][j] = min(f[i-1][j-1], min(f[i][j-1], f[i-1][j]) + 1); else f[i][j] = min(f[i-1][j-1], min(f[i][j-1], f[i-1][j])) + 1; } } } return f[row][col]; }};
阅读全文
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
- 笨方法学Python练习43:你来制作一个游戏
- 线程句柄和线程标识
- iOS NSError详解 NSError错误code对照表 自定义、定制NSError 一看就懂
- 【深度】MySQL集群 对比 SequoiaDB弹性扩展
- mysql函数- 根据roleid,来判断这个roleid在哪个区间中0-100,100-200...
- leetcode 72. Edit Distance
- IDEA常用快捷键
- 奥维奥(AVA)免费为您提供麦肯锡《2016中国汽车消费者报告》
- javascript for/in 循环遍历对象属性
- Ubuntu搭建NFS服务
- 【中国农业银行风险管理部总经理 田继敏】筑牢IT风险第二道防线 保障银行信息科技安全
- 单例模式几种实现
- OpenCV—基本矩阵操作与示例
- List<T>集合转换为EasyUI TreeGrid所需要的Json格式通用工具