【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 ...
随机推荐
- phpexcel图形图表(二)图形
原创文章,转载请注明出处:http://www.cnblogs.com/phpgcs 本篇文章实例演示如上图,画3种图形以及多sheet操作等 先回忆下第一篇中line图形的API调用步骤: 1,引入 ...
- plsql programming 12 集合(忽略, 个人感觉用不到)
关联数组, 嵌套表, varray 个人并不推荐使用集合, 因为操作有别于普通字段. 集合中每一个元素的数据类型都是相同的, 因此这些元素都是同质的(同质元素) 这一章的内容先忽略吧, 因为个人感觉用 ...
- OpenCV中Camshitf算法学习
今天上午,结合OpenCV自带的camshitf例程,简单的对camshitf有了一个大致的认识和理解,现总结如下: 1:关于HSV H指hue(色相).S指saturation(饱和度).V指val ...
- TLS,SSL,HTTPS with Python(转)
From: 扫盲 HTTPS 和 SSL/TLS 协议[0]:引子 需要了解的背景知识: 术语 HTTPS,SSL,TLS 长连接与短连接的关系 了解 CA 证书 基本流程 一.术语扫盲 1.什么是S ...
- 第一百九十四节,jQuery EasyUI,Droppable(放置)组件
jQuery EasyUI,Droppable(放置)组件 学习要点: 1.加载方式 2.属性列表 3.事件列表 4.方法列表 本节课重点了解 EasyUI 中 Droppable(放置)组件的使用方 ...
- python urllib 和 urllib2
urllib 和 urllib2 都是接受URL请求的相关模块,但是提供了不同的功能.两个最显著的不同如下: urllib 仅可以接受URL,不能创建 设置了headers 的Request 类实例: ...
- 绝对定位position: absolute;
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Springboot 1.5.x版本上读取自定义配置文件问题
原来的解决方案: 现在1.5.x以后取消了location地址 1.5以后解决方案:
- of 循环 改变 对象值 对const的理解 对象的字面量 计算属性
const arr = [{a:23,b:34},{a:123,b:134}]console.log(arr)for (let v of arr){console.log(v)const old = ...
- Linux彻底删除mysql5.6
查看安装的mysql组件 rpm -qa | grep -i mysql mysql57-community-release-el6-8.noarch mysql-community-common-5 ...