• 以下为个人翻译方便理解
  • 编辑距离问题是一个经典的动态规划问题。首先定义dp[i][j表示word1[0..i-1]到word2[0..j-1]的最小操作数(即编辑距离)。
  • 状态转换方程有两种情况:边界情况和一般情况,以上表示中 i和j均从1开始(注释:即至少一个字符的字符串向一个字符的字符串转换,0字符到0字符转换编辑距离自然为0)
  • 1.边界情况:将一个字符串转化为空串,很容易看出把word[0...i-1]转化成空串“”至少需要i次操作(注释:i次删除),则其编辑距离为i,即:dp[i][0] = i;dp[0][j] = j.
  • 2.一般情况:转化非空字符串word1[0..i - 1] 为另一非空字符串 word2[0..j - 1],此处问题转化为几个个子问题:假定已知word1[0..i - 2] 到 word2[0..j - 2]的编辑距离, 即dp[i - 1][j - 1],只需考虑word[i - 1] 和 word2[j - 1]

    - 如果 word[i - 1] == word2[j - 1],无需操作即可满足word1[0..i - 1] 与 word2[0..j - 1]相同,则编辑距离dp[i][j] = dp[i - 1][j - 1]

    - 如果 word[i - 1] != word2[j - 1],分为三种子情况:

    - 用 word2[j - 1]替换word1[i - 1],则有 (dp[i][j] = dp[i - 1][j - 1] + 1 (一次操作用于替换));

    - 删除 word1[i - 1] 使得 word1[0..i - 2] = word2[0..j - 1],则有(dp[i][j] = dp[i - 1][j] + 1 (一次操作用于删除));

    - 在word1[0..i - 1] 中插入 word2[j - 1] 使得 word1[0..i - 1] + word2[j - 1] = word2[0..j - 1] ,则有(dp[i][j] = dp[i][j - 1] + 1 (一次操作用于插入)).

    为了保证理解插入和删除带来的细微差别,对于删除,其实是将word1[0..i - 2] 转化成 word2[0..j - 1], 编辑距离是 dp[i - 1][j],之后直接删除word1[i - 1],一次操作,插入也是类似

    (注释:就是由word1[0..i - 2] 编辑转化成 word2[0..j - 1],删除word1[i - 1]两个操作共同完成实现将word1[0..i - 1] 转化成 word2[0..j - 1])

  • 合并规如下:
    • dp[i][0] = i;
    • dp[0][j] = j;
    • dp[i][j] = dp[i - 1][j - 1], if word1[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)
  • 转化为代码如下

    '''

    class Solution {

    public:

    int minDistance(string word1, string word2) {

    int m = word1.length(), n = word2.length();

    vector
  • 你可能会注意到每次更新dp[i][j],我们只需要dp[i - 1][j - 1], dp[i][j - 1], dp[i - 1][j]就行
  • 事实上我们不必维护整个m*n矩阵。相反维护一栏即可,代码空间复杂度降为O(m)或者O(n)取决于你维护的的是矩阵的一行还是一列
  • 优化后代码如下:

    '''

    class Solution {

    public:

    int minDistance(string word1, string word2) {

    int m = word1.length(), n = word2.length();

    vector

leetcode72. Edit Distance(编辑距离)的更多相关文章

  1. Edit Distance编辑距离(NM tag)- sam/bam格式解读进阶

    sam格式很精炼,几乎包含了比对的所有信息,我们平常用到的信息很少,但特殊情况下,我们会用到一些较为生僻的信息,关于这些信息sam官方文档的介绍比较精简,直接看估计很难看懂. 今天要介绍的是如何通过b ...

  2. [LeetCode] Edit Distance 编辑距离

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

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

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

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

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

  5. [leetcode72]Edit Distance(dp)

    题目链接:https://leetcode.com/problems/edit-distance/ 题意:求字符串的最短编辑距离,就是有三个操作,插入一个字符.删除一个字符.修改一个字符,最终让两个字 ...

  6. leetcode72. Edit Distance

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

  7. 【LeetCode每天一题】Edit Distance(编辑距离)

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

  8. 【LeetCode】72. Edit Distance 编辑距离(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 记忆化搜索 动态规划 日期 题目地址:http ...

  9. edit distance(编辑距离,两个字符串之间相似性的问题)

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

随机推荐

  1. SQL Server系统存储过程

    1. execute sp_databases   查询当前所有数据库大致信息(名称,大小等) 2. exec sp_helpdb [Myschool] 返回数据库的所有详细信息(数据库大小,位置,文 ...

  2. 王爽 <<汇编 语言>> 13.6 BIOS中断例程应用

    ;名称:ILOVEU程序 ;使用BIOS提供的中断例程 assume cs:code code segment main: ;显示背景22*80 ;dh中放行号 ;dl中放列号 bibi: push ...

  3. Ajax在html页面获取后台XML文件资源

    一.准备工具 站长吧ASP调试工具.exe,这个工具是为了快速建立asp环境,方便调试. 二.建立文件夹 1.建立网站根文件夹,名字随意,将站长吧ASP调试工具.exe复制到根文件夹: 2.建立xml ...

  4. Angular2学习之开发环境构建

    一.主要资料 http://blog.csdn.net/cz_jjq/article/details/50425206 http://www.tuicool.com/articles/mi6rmuB ...

  5. Node.JS 学习路线图

    转载自:http://www.admin10000.com/document/4624.html 从零开始nodejs系列文章, 将介绍如何利Javascript做为服务端脚本,通过Nodejs框架w ...

  6. PHP在yii2中封装SuperSlide 幻灯片编写自己的SuperSlideWidget的例子

    因为近期给朋友公司做个门户网站,把荒置了6.7年的PHP又重新拾起,发现PHP这些年兴旺多了,很多新的东西看的不明不白,研究了几个框架ZendFramework.thinkphp.Symfony.yi ...

  7. AI(二):人脸识别

    微软提供的人脸识别服务可检测图片中一个或者多个人脸,并为人脸标记出边框,同时还可获得基于机器学习技术做出的面部特征预测.可支持的人脸功能有:年龄.性别.头部姿态.微笑检测.胡须检测以及27个面部重要特 ...

  8. Visual studio智能感知挡住了当前代码输入行

    AssistX->Listboxes->Enable Visual Assist completion, suggestion and member list in .. 如果勾选了该项就 ...

  9. access生成sql脚本,通过VBA调用ADOX

    access生成sql脚本,通过VBA调用ADOX. 使用 MS Access 2016 的VBA,读取mdb文件中的所有表结构(数据类型/长度/精度等),生成对应的SQL create table语 ...

  10. [zz] ROC曲线

    wiki https://zh.wikipedia.org/wiki/ROC%E6%9B%B2%E7%BA%BF 在信号检测理论中,接收者操作特征曲线(receiver operating chara ...