【LeetCode】【动态规划】Edit Distance
描述
Given two words word1 and word2, find the minimum number of operations required to convert word1 to word2.
You have the following 3 operations permitted on a word:
- Insert a character
- Delete a character
- Replace a character
Example 1:
Input: word1 = "horse", word2 = "ros"
Output: 3
Explanation:
horse -> rorse (replace 'h' with 'r')
rorse -> rose (remove 'r')
rose -> ros (remove 'e')
Example 2:
Input: word1 = "intention", word2 = "execution"
Output: 5
Explanation:
intention -> inention (remove 't')
inention -> enention (replace 'i' with 'e')
enention -> exention (replace 'n' with 'x')
exention -> exection (replace 'n' with 'c')
exection -> execution (insert 'u')
思路:动态规划
这是一个经典的动态规划问题,思路参考斯坦福的课程:http://www.stanford.edu/class/cs124/lec/med.pdf
这里把加2变成加1即可
dp[i][0] = i
;dp[0][j] = j
;dp[i][j] = dp[i - 1][j - 1]
, ifword1[i - 1] = word2[j - 1]
;dp[i][j] = min(dp[i - 1][j - 1] + 1, dp[i - 1][j] + 1, dp[i][j - 1] + 1)
, otherwise.
class Solution {
public:
int minDistance(string word1, string word2) {
int m = word1.size(), n = word2.size();
vector<vector<int> > dp(m+, vector<int>(n+, ));
for(int i = ;i<=m;++i)
dp[i][] = i;
for(int i = ;i<=n;++i)
dp[][i] = i;
for(int i = ;i<=m;++i){
for(int j = ;j<=n;++j){
if(word1[i-] == word2[j-])
dp[i][j] = dp[i-][j-];
else
dp[i][j] = min(dp[i-][j-], min(dp[i][j-], dp[i-][j])) + ;
}
}
return dp[m][n];
}
};
【LeetCode】【动态规划】Edit Distance的更多相关文章
- [Leetcode Week8]Edit Distance
Edit Distance 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/edit-distance/description/ Description ...
- [LeetCode] 72. Edit Distance 编辑距离
Given two words word1 and word2, find the minimum number of operations required to convert word1 to ...
- [LeetCode] One Edit Distance 一个编辑距离
Given two strings S and T, determine if they are both one edit distance apart. 这道题是之前那道Edit Distance ...
- 【leetcode】Edit Distance
Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert ...
- Java for LeetCode 072 Edit Distance【HARD】
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- LeetCode One Edit Distance
原题链接在这里:https://leetcode.com/problems/one-edit-distance/ Given two strings S and T, determine if the ...
- leetCode 72.Edit Distance (编辑距离) 解题思路和方法
Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert ...
- [LeetCode] 72. Edit Distance(最短编辑距离)
传送门 Description Given two words word1 and word2, find the minimum number of steps required to conver ...
- LeetCode - 72. Edit Distance
最小编辑距离,动态规划经典题. Given two words word1 and word2, find the minimum number of steps required to conver ...
- 【leetcode】Edit Distance (hard)
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
随机推荐
- plsql programming 10 日期和时间戳
年 月 日 时 分 秒 时区 用小时表示的相对于 UTC 的时差 用分钟表示的相对于 UTC 的时差 date 存储日期和时间, 不带时区, 精确到秒 timestamp 存储日期和时间, 不带时区, ...
- java 获取网页指定内容-2(实践+修改)
import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; ...
- PC如何访问手机网址
http://blog.csdn.net/matthew_fan/article/details/7787504
- js保留几位小数
function reservedDecimal(val, digit) { return Number(val).toFixed(digit);} 调用 reservedDecimal(10,2); ...
- Unity3D学习笔记——游戏组件之Mesh(网格组件)
Mesh:网格组件.主要用于设置外形和外表. Mesh Filter:网格过滤器.就是为游戏对象添加一个外形. 例:设置外形为Sphere 如果获取的网格拥有蒙皮信患,Unity将自动创建一个skn ...
- MySQL运算符总结
注解:常常用于where条件判断中
- cocos2d-x学习日志(18) --程序是怎样開始执行与结束?
问题的由来 怎么样使用 Cocos2d-x 高速开发游戏.方法非常easy,你能够看看其自带的例程,或者从网上搜索教程,执行起第一个HelloWorld,然后在 HelloWorld 里面写相关逻辑代 ...
- NOIP2011提高组(选择客栈)
题目链接:http://codevs.cn/problem/1135/ 题目大意:中文题...就不解释了 题目思路:看了其他巨巨的blog写的,dp思路 #include <iostream&g ...
- Balala Power!(大数+思维)
Balala Power! Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)T ...
- python系列一:python3基础语法
'''python保留字即关键字,我们不能把它们用作任何标识符名称.Python 的标准库提供了一个 keyword 模块,可以输出当前版本的所有关键字: '''>>> import ...