Leetcode72

看起来比较棘手的一道题(列DP方程还是要大胆猜想。。)

DP方程该怎么列呢?

dp[i][j]表示字符串a[0....i-1]转化为b[0....j-1]的最少距离

转移方程分三种情况考虑 分别对应三中操作

因为只需要三个值就可以更新dp[i][j] 我们可以把空间复杂度降低到O(n)

  1. Replace word1[i - 1] by word2[j - 1] (dp[i][j] = dp[i - 1][j - 1] + 1 (for replacement));
  2. Delete word1[i - 1] and word1[0..i - 2] = word2[0..j - 1] (dp[i][j] = dp[i - 1][j] + 1 (for deletion));
  3. Insert word2[j - 1] to word1[0..i - 1] and word1[0..i - 1] + word2[j - 1] = word2[0..j - 1] (dp[i][j] = dp[i][j - 1] + 1 (for insertion)).
    class Solution {
    public:
    int minDistance(string word1, string word2) {
    int m = word1.length(), n = word2.length();
    vector<int> cur(m + 1, 0);
    for (int i = 1; i <= m; i++)
    cur[i] = i;
    for (int j = 1; j <= n; j++) {
    int pre = cur[0];
    cur[0] = j;
    for (int i = 1; i <= m; i++) {
    int temp = cur[i];
    if (word1[i - 1] == word2[j - 1])
    cur[i] = pre;
    else cur[i] = min(pre + 1, min(cur[i] + 1, cur[i - 1] + 1));
    pre = temp;
    }
    }
    return cur[m];
    }
    };

      

第十八周 Leetcode 72. Edit Distance(HARD) O(N^2)DP的更多相关文章

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

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

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

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

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

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

  4. LeetCode - 72. Edit Distance

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

  5. [leetcode]72. Edit Distance 最少编辑步数

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

  6. [leetcode] 72. Edit Distance (hard)

    原题 dp 利用二维数组dp[i][j]存储状态: 从字符串A的0~i位子字符串 到 字符串B的0~j位子字符串,最少需要几步.(每一次删增改都算1步) 所以可得边界状态dp[i][0]=i,dp[0 ...

  7. 【Leetcode】72 Edit Distance

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

  8. 刷题72. Edit Distance

    一.题目说明 题目72. Edit Distance,计算将word1转换为word2最少需要的操作.操作包含:插入一个字符,删除一个字符,替换一个字符.本题难度为Hard! 二.我的解答 这个题目一 ...

  9. 达拉草201771010105《面向对象程序设计(java)》第十八周学习总结

    达拉草201771010105<面向对象程序设计(java)>第十八周学习总结 实验十八  总复习 实验时间 2018-12-30 1.实验目的与要求 (1) 综合掌握java基本程序结构 ...

随机推荐

  1. bzoj 2721[Violet 5]樱花 数论

    [Violet 5]樱花 Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 671  Solved: 395[Submit][Status][Discuss ...

  2. 使现有的VSCode成为便携版(绿色版)

    VSCode可以说是各种代码编辑器前端之中的神器了,相对体积小且扩展性强,我们希望将它携带在U盘中在各种工作环境中使用,官方也提供了在Windows,Linux和MacOS三大平台中使VSCode便携 ...

  3. 将登录等信息保存到session中和退出session

    JShop简介:jshop是一套使用Java语言开发的B2C网店系统,致力于为个人和中小企业提供免费.好用的网店系统. 项目主页:http://git.oschina.net/dinguangx/js ...

  4. 【Educational Codeforces Round 48】

    A:https://www.cnblogs.com/myx12345/p/9843001.html B:https://www.cnblogs.com/myx12345/p/9843021.html ...

  5. 事件和委托:第 5 页 委托、事件与Observer设计模式

    原文发布时间为:2008-11-01 -- 来源于本人的百度文章 [由搬家工具导入] 委托、事件与Observer设计模式 范例说明 上面的例子已不足以再进行下面的讲解了,我们来看一个新的范例,因为之 ...

  6. Arduino学习笔记0---开发板的了解

    Arduino的入门文档https://wenku.baidu.com/view/4040861d58fafab069dc02d4.html?from=search,共61页的文档,看完就差不多可以入 ...

  7. iOS 调用系统相册 相机 时,显示中文标题

    解决手机语言已经设置显示中文 在调用系统相册.相机界面 时显示英文问题, 在 info.plist里面添加Localized resources can be mixed YES 表示是否允许应用程序 ...

  8. Tomcat绑定具体IP

    https://blog.csdn.net/paomadeng/article/details/1826880

  9. 105. Construct Binary Tree from Inorder and preorder Traversal

    /* * 105. Construct Binary Tree from Inorder and preorder Traversal * 11.20 By Mingyang * 千万不要以为root ...

  10. OSGI是什么

    OSGI(Open Services Gateway Initiative),或者通俗点说JAVA动态模块系统,定义了一套模块应用开发的框架.OSGI容器实现方案如Knopflerfish, Equi ...