描述

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:

  1. Insert a character
  2. Delete a character
  3. 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即可

  1. dp[i][0] = i;
  2. dp[0][j] = j;
  3. dp[i][j] = dp[i - 1][j - 1], if word1[i - 1] = word2[j - 1];
  4. 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的更多相关文章

  1. [Leetcode Week8]Edit Distance

    Edit Distance 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/edit-distance/description/ Description ...

  2. [LeetCode] 72. Edit Distance 编辑距离

    Given two words word1 and word2, find the minimum number of operations required to convert word1 to  ...

  3. [LeetCode] One Edit Distance 一个编辑距离

    Given two strings S and T, determine if they are both one edit distance apart. 这道题是之前那道Edit Distance ...

  4. 【leetcode】Edit Distance

    Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert  ...

  5. 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 ...

  6. LeetCode One Edit Distance

    原题链接在这里:https://leetcode.com/problems/one-edit-distance/ Given two strings S and T, determine if the ...

  7. leetCode 72.Edit Distance (编辑距离) 解题思路和方法

    Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert  ...

  8. [LeetCode] 72. Edit Distance(最短编辑距离)

    传送门 Description Given two words word1 and word2, find the minimum number of steps required to conver ...

  9. LeetCode - 72. Edit Distance

    最小编辑距离,动态规划经典题. Given two words word1 and word2, find the minimum number of steps required to conver ...

  10. 【leetcode】Edit Distance (hard)

    Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...

随机推荐

  1. AutoLayout详解+手把手实战(转载)

    首先说一下这篇博客虽然是标记为原创,但是事实并非本人亲自写出来的,知识点和例子本人花了一天各处查 找和整理最终决定写一个汇总的详解,解去各位朋友到处盲目查找的必要,因为不是转载某一个人的内容,故此不标 ...

  2. 华为HiAI 助力苏宁易购,让你尽享完美视觉购物体验!

    还在感慨商品照片与实物存在差距,又要退货? 还在抱怨被忽视的图片小细节,影响了生活品质? 想要“买买买”, 又担心海量的商品图片耗光你的流量? 就在近期 搭载HiAI能力的苏宁易购新版上线, 让你畅快 ...

  3. 面向对象JSON的继承(复制)与函数的继承(复制)

    今天这里和大家分享下如何复制对象 的属性 创建 对象的方式有三种,这里和大家分享下最常用的几种 1.JSON格式的方式创建对象 2.用函数的方式创建,然后用new关键字实例化对象,关于this的指向问 ...

  4. HTTP 请求:GET vs. POST

    两种在客户端和服务器端进行请求-响应的常用方法是:GET 和 POST. GET - 从指定的资源请求数据 POST - 向指定的资源提交要处理的数据 GET 基本上用于从服务器获得(取回)数据.注释 ...

  5. 【转】ATL提供的所有转换宏

    在头文件<atlconv.h>中定义了ATL提供的所有转换宏,如: A2CW (LPCSTR) -> (LPCWSTR) A2W     (LPCSTR) -> (LPWSTR ...

  6. react import改为绝对路径

    最近在使用react时发现路径用../../很不方便,特别是修改项目结构时,加减../都能改到吐血, 所有在网上找了半天webpack的配置,特此记录下 module.exports = (webpa ...

  7. [Oracle] - Create DB on Oracle 12c for an Application

    Let's say we are going to develop a application for a bank, or any other enterprise, this applicatio ...

  8. tomcat开启远程调试

    tomcat开启远程调试模式: 需要在启动脚本中的 JAVA_OPTS='-server -Xms1024m -Xmx1024m -Xmn384m -Xss256k -XX:PermSize=128m ...

  9. java.lang.ClassFormatError: Truncated class file

    之前跑的很好的程序,因为我本地IDE出了问题的原因,倒是编译的错误的class文件,结果点击的时候报这样的错误,后来重新clean了工程,重新打包解压启动,问题依旧. 解决办法: 把tomcat的wo ...

  10. undefined let 作用域

    const o = {uid:123,pid:'wwww'}const wxPayNotifyUrlBizInfo = (o) => { // TODO json let s = '' for ...