链接:https://leetcode-cn.com/problems/edit-distance/submissions/

设dp[i][j]表示串s1前i个字符变换成串s2前j个字符所需要的最小操作次数。

首先要初始化dp数组的第一行和第一列  。

dp[ i ][ j ]分为四种状态:

1. s1[ i ] == s2[ j ] 此时不需要做变化,dp[ i ] [ j ] = dp[ i -1 ] [ j -1 ]。

2. s1[ i ] ! = s2[ j ]分为三种情况:

第一种:删除s1[ i ], 那么dp[ i ][ j ]  = dp [ i -1 ] [ j ] + 1

第二种:替换s1[ i ],那么dp[ i ] [ j ]  = dp [ i -1 ] [ j -1 ] + 1

第三种:插入元素,那么dp[ i ] [ j ] = dp[ i ] [ j -1 ] + 1

三种情况的操作取min值为dp[ i ] [ j ] 的新状态

AC代码:

class Solution {
public:
int minDistance(string word1, string word2) {
int dp[word1.length()+1][word2.length()+1];
dp[0][0] = 0;
for(int i = 1;i<=word2.length();i++){
dp[0][i] = i;
}
for(int i = 1;i<=word1.length();i++){
dp[i][0] = i;
}
for(int i = 1;i<=word1.length();i++){
for(int j = 1;j<=word2.length();j++){
if(word1[i-1] == word2[j-1]){
dp[i][j] = dp[i-1][j-1];
}
else{
dp[i][j] = min(dp[i-1][j-1],min(dp[i-1][j],dp[i][j-1]))+1;
}
}
}
return dp[word1.length()][word2.length()];
}
};

leetcode 72.编辑距离(dp)的更多相关文章

  1. [LeetCode]72. 编辑距离(DP)

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

  2. Java实现 LeetCode 72 编辑距离

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

  3. [leetcode] 72. 编辑距离(二维动态规划)

    72. 编辑距离 再次验证leetcode的评判机有问题啊!同样的代码,第一次提交超时,第二次提交就通过了! 此题用动态规划解决. 这题一开始还真难到我了,琢磨半天没有思路.于是乎去了网上喵了下题解看 ...

  4. [LeetCode] 72. 编辑距离 ☆☆☆☆☆(动态规划)

    https://leetcode-cn.com/problems/edit-distance/solution/bian-ji-ju-chi-mian-shi-ti-xiang-jie-by-labu ...

  5. [Leetcode 72]编辑距离 Edit Distance

    [题目] Given two words word1 and word2, find the minimum number of operations required to convert word ...

  6. leetcode 72. 编辑距离

    /***** 定义状态: DP[i][j]其中i表示word1前i个字符,j表示Word2前i个字符 DP[i][j]表示单词1前i个字符匹配单词2前j个字符,最少变换次数: 状态转移: for i: ...

  7. 第30章 LeetCode 72 编辑距离

    每日一句 A flower cannot blossom without sunshine, and man cannot live without love. 花没有阳光就不能盛开,人没有爱就不能生 ...

  8. leetcode 72 编辑距离 JAVA

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

  9. Leetcode之动态规划(DP)专题-72. 编辑距离(Edit Distance)

    Leetcode之动态规划(DP)专题-72. 编辑距离(Edit Distance) 给定两个单词 word1 和 word2,计算出将 word1 转换成 word2 所使用的最少操作数 . 你可 ...

随机推荐

  1. SpringBoot的应运而生

    随着动态语言的流行(Ruby,Groovy,Scala,Node.js),java的开发显得格外的笨重,繁多的配置,低下的效率,复杂的部署流程以及第三方技术集成难度大.springboot应运而生,使 ...

  2. VMware安装centos7与配置网络

    自己想搭建个虚机学习下k8s,使用VMware安装centos7,上不了网,折腾了很久才连上.发现网上很多教程都是错误的或者不明确的,这边写下自己的配置记录 首先安装centos7系统就不赘述了,这边 ...

  3. 创建基于ASP.NET core 3.1 的RazorPagesMovie项目(三)-已搭建基架的Razor页面解释和更新

    本节主要介绍在上一节中通过搭建基架而创建的Razor页面,并做一些UI改变. 一.创建.删除.详细信息和编辑页面 1.双击Pages/Movies/Index.cshtml.cs文件,这是一个Razo ...

  4. Extended Traffic LightOJ - 1074 spfa判断负环

    //判断负环 在负环内的城市输出? #include <iostream> #include <queue> #include <cstdio> #include ...

  5. 《深入理解java虚拟机》读书笔记十——第十一章

    第十一章  晚期(运行期)优化 1.HotSpot虚拟机内的即时编译 解释器与编译器: 许多Java虚拟机的执行引擎在执行Java代码的时候都有解释执行(通过解释器执行)和编译执行(通过即时编译器产生 ...

  6. Java数组动态增加容量

    Java数组初始化需要指定数组容量,但是在许多情况下需要动态扩充容量.有两种方法可以实现:1.采用ArrayList类数组,它可以在需要时自动扩容:2.采用System.arraycopy方法实现,其 ...

  7. Codeforce 584A - Olesya and Rodion

    Olesya loves numbers consisting of n digits, and Rodion only likes numbers that are divisible by t. ...

  8. LED Holiday Light-5 Mm Wide Angle Cone Lights: Pros

    But in rare cases, the opposite is true: the opinions of consultants are so synchronized that it is ...

  9. Selenium3+python自动化006-环境搭建

    一.selenium简介 1.Selenium历史 (1)Selenium 1.0: Selenium IDE      Selenium Grid    Selenium RC(核心功能) Sele ...

  10. C语言-无符号数与有符号数不为人知的秘密

    一.无符号数与有符号数 1.计算机中的符号位 数据类型的最高位用于标识数据的符号 -最高位为1,表明这个数为负数 -最高位为0,表明这个数为正数 #include <stdio.h> in ...