LeetCode 72. Edit Distance

参考链接:https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-72-edit-distance/
思路:

Solution1:
递归

// Author: Huahua
// Runtime: 13 ms
class Solution {
public:int minDistance(string word1, string word2) {int l1 = word1.length();int l2 = word2.length();d_ = vector<vector<int>>(l1 + 1, vector<int>(l2 + 1, -1));return minDistance(word1, word2, l1, l2);}
private:vector<vector<int>> d_;// minDistance from word1[0:l1-1] to word2[0:l2-1]int minDistance(const string& word1, const string& word2, int l1, int l2) {if (l1 == 0) return l2;if (l2 == 0) return l1;if (d_[l1][l2] >= 0) return d_[l1][l2];int ans;if (word1[l1 - 1] == word2[l2 - 1])ans = minDistance(word1, word2, l1 - 1, l2 - 1);else ans = min(minDistance(word1, word2, l1 - 1, l2 - 1),min(minDistance(word1, word2, l1 - 1, l2), minDistance(word1, word2, l1, l2 - 1))) + 1;return d_[l1][l2] = ans;        }
};

Solution2:
迭代

// Author: Huahua
// Runtime: 9 ms
class Solution {
public:int minDistance(string word1, string word2) {int l1 = word1.length();int l2 = word2.length();// d[i][j] := minDistance(word1[0:i - 1], word2[0:j - 1]);vector<vector<int>> d(l1 + 1, vector<int>(l2 + 1, 0));for (int i = 0; i <= l1; ++i)d[i][0] = i;for (int j = 0; j <= l2; ++j)d[0][j] = j;for (int i = 1; i <= l1; ++i)for (int j = 1; j <= l2; ++j) {int c = (word1[i - 1] == word2[j - 1]) ? 0 : 1;d[i][j] = min(d[i - 1][j - 1] + c, min(d[i][j - 1], d[i - 1][j]) + 1);}return d[l1][l2];}
};

PS:花花真牛逼。。。

【重点!记忆化递归+DP】LeetCode 72. Edit Distance相关推荐

  1. LeetCode 87. 扰乱字符串(记忆化递归 / DP)

    文章目录 1. 题目 2. 解题 2.1 记忆化递归 2.2 动态规划 1. 题目 给定一个字符串 s1,我们可以把它递归地分割成两个非空子字符串,从而将其表示为二叉树. 下图是字符串 s1 = &q ...

  2. LeetCode 514. 自由之路(记忆化递归 / DP)

    文章目录 1. 题目 2. 解题 1. 题目 电子游戏"辐射4"中,任务"通向自由"要求玩家到达名为"Freedom Trail Ring" ...

  3. 【记忆化递归+DP】LeetCode 139. Word Break

    LeetCode 139. Word Break Solution1: 记忆化递归的典型套路题 参考网址:https://zxi.mytechroad.com/blog/leetcode/leetco ...

  4. 【DFS + 记忆化递归 + DP】LeetCode 91. Decode Ways

    LeetCode 91. Decode Ways Solution1:我的答案 还是记录一下,最容易想到的是DFS,但是在第223/238个case上就超时了... class Solution { ...

  5. 【DFS + 记忆化递归】LeetCode 140. Word Break II

    LeetCode 140. Word Break II Solution1:我的答案 纯DFS,在第31个case时超时,还是记录一下.. class Solution { // DFS public ...

  6. LeetCode(72)Edit Distance

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

  7. [leetcode] 72.Edit Distance 编辑距离-史前最简明清晰的解答

    题目: 给定两个单词 word1 和 word2,计算出将 word1 转换成 word2 所使用的最少操作数 . 你可以对一个单词进行如下三种操作: 插入一个字符 删除一个字符 替换一个字符 输入: ...

  8. 【重点!DFS/记忆化递归 + BFS】LeetCode 133. Clone Graph

    LeetCode 133. Clone Graph Solution1: DFS/记忆化递归,参考网址:http://www.cnblogs.com/grandyang/p/4267628.html ...

  9. LeetCode 337. 打家劫舍 III(记忆化+递归)

    文章目录 1. 题目 1.1 相关题目: 2. 解题 2.1 递归 2.2 记忆化递归 1. 题目 在上次打劫完一条街道之后和一圈房屋后,小偷又发现了一个新的可行窃的地区.这个地区只有一个入口,我们称 ...

最新文章

  1. ida pro 7.5 idapython学习
  2. 活在无尽梦境的后续 β
  3. java 封箱_java封箱和拆箱分析
  4. 面条html5,使用 babel 全家桶模块化古老的面条代码
  5. 指标公式c语言源码下载,自用60分钟指标源码
  6. 数据结构基础知识(2)
  7. shell脚本不换行刷新数据
  8. C#常见算法题目(面试准备)
  9. VTP与三层交换配置实验
  10. java项目中遇到的幂等性问题
  11. 【学习随记】Word域代码相关
  12. 征途mysql启动不了_mysql无法启动
  13. 有关一道身份证的python编程题
  14. BW随手记-项目上零碎总结(SAP销售,开票,获利能力分析)
  15. 快速入门了解后端网络方面必备知识
  16. VBox 启动虚拟机失败 - NtCreateFile
  17. python输出去空格_python不空格
  18. ICC2: secondary pg pin的作用与连接
  19. 姿态估计之2D人体姿态估计(1)(仅供个人参考)
  20. svn 禁止访问的问题

热门文章

  1. PAD-Net: Multi-Tasks Guided Prediction-and-Distillation Network for Simultaneous Depth Estimation an
  2. WordPress学习笔记(二)插件安装
  3. 卡尔曼滤波器的一种形象表达
  4. html编辑器 开发原理,在线所见即所得HTML编辑器的实现原理浅析
  5. vue实现5秒后自动隐藏_王者荣耀:玩家选中单妲己被5楼疯狂嘲讽,发出战绩后,秒变舔狗...
  6. python public_python中private、protectedamp;public
  7. qt 对话框位置如何确定_便利店如何确定收银台位置?
  8. html5 app 原理,html5打包成app应用的原理是什么?
  9. 搜索接口php,【微信公众平台开发】百度周边搜索接口php封装
  10. 二级c语言题库手机软件,C语言二级题库下载