leetcode 72.编辑距离(dp)
链接: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)的更多相关文章
- [LeetCode]72. 编辑距离(DP)
题目 给定两个单词 word1 和 word2,计算出将 word1 转换成 word2 所使用的最少操作数 . 你可以对一个单词进行如下三种操作: 插入一个字符 删除一个字符 替换一个字符 示例 1 ...
- Java实现 LeetCode 72 编辑距离
72. 编辑距离 给定两个单词 word1 和 word2,计算出将 word1 转换成 word2 所使用的最少操作数 . 你可以对一个单词进行如下三种操作: 插入一个字符 删除一个字符 替换一个字 ...
- [leetcode] 72. 编辑距离(二维动态规划)
72. 编辑距离 再次验证leetcode的评判机有问题啊!同样的代码,第一次提交超时,第二次提交就通过了! 此题用动态规划解决. 这题一开始还真难到我了,琢磨半天没有思路.于是乎去了网上喵了下题解看 ...
- [LeetCode] 72. 编辑距离 ☆☆☆☆☆(动态规划)
https://leetcode-cn.com/problems/edit-distance/solution/bian-ji-ju-chi-mian-shi-ti-xiang-jie-by-labu ...
- [Leetcode 72]编辑距离 Edit Distance
[题目] Given two words word1 and word2, find the minimum number of operations required to convert word ...
- leetcode 72. 编辑距离
/***** 定义状态: DP[i][j]其中i表示word1前i个字符,j表示Word2前i个字符 DP[i][j]表示单词1前i个字符匹配单词2前j个字符,最少变换次数: 状态转移: for i: ...
- 第30章 LeetCode 72 编辑距离
每日一句 A flower cannot blossom without sunshine, and man cannot live without love. 花没有阳光就不能盛开,人没有爱就不能生 ...
- leetcode 72 编辑距离 JAVA
题目: 给定两个单词 word1 和 word2,计算出将 word1 转换成 word2 所使用的最少操作数 . 你可以对一个单词进行如下三种操作: 插入一个字符 删除一个字符 替换一个字符 示例 ...
- Leetcode之动态规划(DP)专题-72. 编辑距离(Edit Distance)
Leetcode之动态规划(DP)专题-72. 编辑距离(Edit Distance) 给定两个单词 word1 和 word2,计算出将 word1 转换成 word2 所使用的最少操作数 . 你可 ...
随机推荐
- Sentence by defender
也许,人在旅途,不是你看清了多少事,而是你看轻了多少事.
- div中宽高度自适应文字换行居中问题解决
<html> <head> <meta charset="UTF-8"/> <title>div中宽高度自适应文字换行居中demo& ...
- 《深入理解java虚拟机》读书笔记四——第五章
第五章 调优案例分析与实战
- Unknown CMake command "check_symbol_exists".
- Using these message generators: gencpp;geneus;genlisp;gennodejs;genpyCMake Error at CMakeLists.txt ...
- SQLserver 数据类型转换
1:CAST 方法 CAST(任何有效的表达试 AS 要转换的数据类型 [数据类型的长度,可选]) 例:SELECT CAST(10000 as varchar(10)) SELECT CAS ...
- Qt Gui 第一章~第二章
一.Qt启动 qmake -project; 创建xxx.pro qmake xxx.pro; 生成makefile文件 make:构建该程序,生成可执行文件 运行程序:windows:xxx:mac ...
- jquery 清除内容
jQuery empty() 方法删除被选元素的子元素. $("#div1").empty(); 清空文本框的值 $("#password").val(&quo ...
- 字符串匹配算法--KMP字符串搜索(Knuth–Morris–Pratt string-searching)C语言实现与讲解
一.前言 在计算机科学中,Knuth-Morris-Pratt字符串查找算法(简称为KMP算法)可在一个主文本字符串S内查找一个词W的出现位置.此算法通过运用对这个词在不匹配时本身就包含足够的信息 ...
- fastadmin 框架中图片点击放大
fastadmin的原生图片预览,重新打开一个窗口太麻烦,使用layui做一个弹窗式的图片预览 1.将下面代码放在backend-init.js文件中 $('body').on('click', '[ ...
- Ice Skating
Bajtek is learning to skate on ice. He's a beginner, so his only mode of transportation is pushing o ...