题目链接:https://leetcode.com/problems/edit-distance/description/

题目大意:找出两个字符串之间的编辑距离(每次变化都只消耗一步)。

法一(借鉴):经典dp。代码如下(耗时15ms):

     //dp公式:dp[i][j]表示第一个字符串前i个字符到第二个字符串前j个字符的编辑距离长度
//当word1[i]==word2[j]时,dp[i][j]=dp[i-1][j-1]
//否则,dp[i][j]=min(dp[i-1][j],dp[i][j-1],dp[i-1][j-1])+1
public int minDistance(String word1, String word2) {
int len1 = word1.length(), len2 = word2.length();
int dp[][] = new int[len1+1][len2+1];
//初始化
for(int i = 0; i <= len1; i++) {
dp[i][0] = i;
}
for(int i = 0; i <= len2; i++) {
dp[0][i] = i;
}
for(int i = 1; i <= len1; i++) {//下标从1开始
for(int j = 1; j <= len2; j++) {
if(word1.charAt(i - 1) == word2.charAt(j - 1)) {
dp[i][j] = dp[i - 1][j - 1];
}
else {
int min = Integer.MAX_VALUE;
if(min > dp[i - 1][j - 1]) {
min = dp[i - 1][j - 1];
}
if(min > dp[i][j - 1]) {
min = dp[i][j - 1];
}
if(min > dp[i - 1][j]) {
min = dp[i - 1][j];
}
dp[i][j] = min + 1;
}
}
}
return dp[len1][len2];
}

dp数组变化(例子:abc到acde的编辑距离):

0 1("a") 2("c") 3("d") 4("e")
1("a") 0(a->a) 1(a->ac) 2(a->acd) 3(a->acde)
2("b") 1(ab->a) 1(ab->ac) 2(ab->acd) 3(ab->acde)
3("c") 2(abc->a) 1(abc->ac) 2(abc->acd) 3(abc->acde)

从上表可清楚看见最后结果在dp[3][4]中。

dp数组填充顺序:从左上到右下,即每一次数值计算都要用到左边,上边,左上的数据。

72.Edit Distance---dp的更多相关文章

  1. 【Leetcode】72 Edit Distance

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

  2. 刷题72. Edit Distance

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

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

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

  4. 72. Edit Distance

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

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

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

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

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

  7. 72. Edit Distance(困难,确实挺难的,但很经典,双序列DP问题)

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

  8. 72. Edit Distance (String; DP)

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

  9. [leetcode DP]72. Edit Distance

    计算最少用多少不把word1变为word2, 思路:建立一个dp表,行为word1的长度,宽为word2的长度 1.边界条件,dp[i][0] = i,dp[0][j]=j 2.最优子问题,考虑已经知 ...

  10. 第十八周 Leetcode 72. Edit Distance(HARD) O(N^2)DP

    Leetcode72 看起来比较棘手的一道题(列DP方程还是要大胆猜想..) DP方程该怎么列呢? dp[i][j]表示字符串a[0....i-1]转化为b[0....j-1]的最少距离 转移方程分三 ...

随机推荐

  1. iOS----MRC(手动内存管理)

    1.MRC是什么,有什么用? 在苹果开发中,我们是没有垃圾回收机制的.所以在ARC推出之前,我们苹果开发程序员需要通过手动代码的形式尽量严密的管理我们的App的内存: ---------------- ...

  2. Hadoop基于Protocol Buffer的RPC实现代码分析-Server端--转载

    原文地址:http://yanbohappy.sinaapp.com/?p=110 最新版本的Hadoop代码中已经默认了Protocol buffer(以下简称PB,http://code.goog ...

  3. 我的bootstrap学习

    前端开发框架bootstrap Bootstrap 安装     <link ref="stylesheet" href="bs/css/bootstrap.css ...

  4. 【JavaScript&jQuery】轮展图

    用bootstrap实现轮展图和用Jquery自定义轮展图两种   1.使用bootstrap插件 效果图: 用一个超简单的方法,那就是用bootstrap的插件,什么?不懂bootstrap?没关系 ...

  5. 深入理解JVM一类加载器原理

    我们知道我们编写的java代码,会经过编译器编译成字节码文件(class文件),再把字节码文件装载到JVM中,映射到各个内存区域中,我们的程序就可以在内存中运行了.那么字节码文件是怎样装载到JVM中的 ...

  6. 【题解】AC自动机题解合集

    最近貌似大家都在搞字符串?很长一段时间都没有写博客了……还是补一补坑吧. 感觉AC自动机真的非常优美了,通过在trie树上建立fail指针可以轻松解决多模匹配的问题.实际上在AC自动机上的匹配可以看做 ...

  7. 聊聊flink的CsvTableSource

    序 本文主要研究一下flink的CsvTableSource TableSource flink-table_2.11-1.7.1-sources.jar!/org/apache/flink/tabl ...

  8. 【BZOJ4540】【HNOI2016】序列(莫队)

    [BZOJ4540][HNOI2016]序列(莫队) 题面 BZOJ 洛谷 Description 给定长度为n的序列:a1,a2,-,an,记为a[1:n].类似地,a[l:r](1≤l≤r≤N)是 ...

  9. BZOJ2716:[Violet 3]天使玩偶——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=2716 样例输入 2 31 12 32 1 21 3 32 4 2 样例输出 1 2 ———————— ...

  10. position:fixed 相对父元素定位

    position:fixed是对于浏览器窗口定位的,要实现相当于父元素定位,可以这样: 不设置fixed元素的top,bottom,left,right,只设置margin来实现. 这种方法本质上fi ...