• 以下为个人翻译方便理解
  • 编辑距离问题是一个经典的动态规划问题。首先定义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. 【P1379】天才的约数和

    来自GDOI2007,原题已不可考-- 又自己做出来了好开心,找特殊性是个关键的切入点 原题: 这天周航遇到了靳泽旭. 周航:"我是天才!" 靳泽旭:"你为什么是天才?& ...

  2. CocoaPods创建私有pods

    由于项目需求,需要把项目的不同模块拆分出来即 组件化 ,一开始想做成多target模式,后来换成私有pods CocoaPods的安装和使用,网上很多,自行搜索即可. 听说可以基于svn创建pod私有 ...

  3. js 浏览器兼容的一些方法

    使用js是一件令人很抓狂的事情,很多的浏览器兼容,一大推的代码,谁的脑袋能记住那么多的东西,只有平时多积累,所谓熟能生巧嘛..这里列出一些常用的兼容代码,一点点积累哈~~~     一.以跨浏览器的方 ...

  4. Python爬虫库Scrapy入门1--爬取当当网商品数据

    1.关于scrapy库的介绍,可以查看其官方文档:http://scrapy-chs.readthedocs.io/zh_CN/latest/ 2.安装:pip install scrapy  注意这 ...

  5. 免费SSL-HTTS 申请与配置 NGINX配置

    Let's Encrypt是很火的一个免费SSL证书发行项目,自动化发行证书,证书有90天的有效期.适合个人使用或者临时使用,不用再忍受自签发证书不受浏览器信赖的提示.Let's Encrypt已经发 ...

  6. Oracle 11g r2 安装

    Help Center:http://docs.oracle.com/cd/E11882_01/install.112/e24326/toc.htm#i1011296 前提:linux需要安装图形化介 ...

  7. 委托、回调 Lambda表达式书写方式

  8. win7 64位 VS2010调试提示“ORA-12154: TNS: 无法解析指定的连接标识符”的解决方法

    如果使用工具可以连接Oralce说明与Oralce安装无法. 解决方法: 解决步骤: 1.去网上下载"instantclient-basic-win32-11.2.0.1.0.zip&quo ...

  9. bzoj3316: JC loves Mkk

    Description Input 第1行,包含三个整数.n,L,R.第2行n个数,代表a[1..n]. Output 仅1行,表示询问答案.如果答案是整数,就输出整数:否则,输出既约分数“P/Q”来 ...

  10. js api 实现钉钉免登

    js api 实现钉钉免登,用于从钉钉微应用跳转到企业内部的oa,erp等,我刚刚实施完了我公司的这个功能,钉钉用起来还不错. 1 js api 实现钉钉免登,页面配置. <title>利 ...