• 以下为个人翻译方便理解
  • 编辑距离问题是一个经典的动态规划问题。首先定义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. 3.基础点的移动web

    1.移动端的屏幕适配 在Web开发中可以使用px(像素).em.pt(点).in(英寸).cm(厘米)做为长度单位,我们最常用px(像素)做为长度单位. 我们可以将上述的几种长度单位划分成相对长度单位 ...

  2. RecyclerView使用

    步骤: 1:首先导入V7依赖包 2:在布局中引用RecyclerView 3:在activity中查找控件 4:继承RecyclerView.Adapter,实现它的3个方法, (1):加载布局的on ...

  3. BingHack,Bing旁注API查询工具

    现在旁注查询都失效了.通过网上查询发现有人说可以通过微软的API进行旁注查询 https://datamarket.azure.com/dataset/explore/bing/search 注册登录 ...

  4. ListView的属性及方法详解

    本文转载于:http://blog.csdn.net/vector_yi/article/details/23195411 近期在重新学习Android控件知识,目前进行到ListView,感觉这是一 ...

  5. Python之路,day3-Python基础

    三级菜单 menu = { '北京':{ '海淀':{ '五道口':{ 'soho':{}, '网易':{}, 'google':{} }, '中关村':{ '爱奇艺':{}, '汽车之家':{}, ...

  6. 安卓,支付宝app登录时,提示 服务器安全证书已过期或不可信任,请问怎么解决

    安卓,支付宝app登录时,提示 服务器安全证书已过期或不可信任,请问怎么解决 请把手机时间调成当前时间.

  7. linux下shell编写九九乘法表

    主要语法:类似    1x2       echo   $((1*2)) for 变量 in 值1 值2 值3 ;do linux命令或者语句done

  8. gRaphael——JavaScript 矢量图表库:两行代码实现精美图表

    gRaphael 是一个致力于帮助开发人员在网页中绘制各种精美图表的 Javascript 库,基于强大的 Raphael 矢量图形库.你只需要编写几行简单的代码就能创建出精美的条形图.饼图.点图和曲 ...

  9. AngularJS的MVC中C的实现

    Angular的MVC中C的实现 注:在controller操作Dom效率是很低的,我们使用封装的指令去操作Dom

  10. aar引用 no executable code found问题

    主工程中 repositories { flatDir { dirs project(':trade_module').file('libs') }} dependencies { compile f ...