72.Edit Distance---dp
题目链接: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的更多相关文章
- 【Leetcode】72 Edit Distance
72. Edit Distance Given two words word1 and word2, find the minimum number of steps required to conv ...
- 刷题72. Edit Distance
一.题目说明 题目72. Edit Distance,计算将word1转换为word2最少需要的操作.操作包含:插入一个字符,删除一个字符,替换一个字符.本题难度为Hard! 二.我的解答 这个题目一 ...
- [LeetCode] 72. Edit Distance 编辑距离
Given two words word1 and word2, find the minimum number of operations required to convert word1 to ...
- 72. Edit Distance
题目: Given two words word1 and word2, find the minimum number of steps required to convert word1 to w ...
- [LeetCode] 72. Edit Distance(最短编辑距离)
传送门 Description Given two words word1 and word2, find the minimum number of steps required to conver ...
- leetCode 72.Edit Distance (编辑距离) 解题思路和方法
Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert ...
- 72. Edit Distance(困难,确实挺难的,但很经典,双序列DP问题)
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- 72. Edit Distance (String; DP)
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- [leetcode DP]72. Edit Distance
计算最少用多少不把word1变为word2, 思路:建立一个dp表,行为word1的长度,宽为word2的长度 1.边界条件,dp[i][0] = i,dp[0][j]=j 2.最优子问题,考虑已经知 ...
- 第十八周 Leetcode 72. Edit Distance(HARD) O(N^2)DP
Leetcode72 看起来比较棘手的一道题(列DP方程还是要大胆猜想..) DP方程该怎么列呢? dp[i][j]表示字符串a[0....i-1]转化为b[0....j-1]的最少距离 转移方程分三 ...
随机推荐
- C++解析(19):函数对象、关于赋值和string的疑问
0.目录 1.函数对象 2.重载赋值操作符 3.string类 4.小结 1.函数对象 编写一个函数: 函数可以获取斐波那契数列每项的值 每调用一次返回一个值 函数可根据需要重复使用 实现功能: #i ...
- linux系统常见命令以及操作
2.安装xshell,安装完打开,配置回话,输入名称(随便).SSH.主机(打开linux,点击右上角电脑图标system etho进行联网,打开终端输入ifconfig回车,找到inet add地址 ...
- 口胡:[HNOI2011]数学作业
题面 一开始看这题看了好久--觉得这题不可做. 结果是看错题了,我居然看着一段长长的C开头的单词,然后就觉得这是卡特兰数--不知道我在想些什么-- 观察到对于 i = 1~9 : f[i] = f[i ...
- 各行业最受欢迎的编程语言,硬件最青睐C和C++
近日,HackerRank发布了2018年开发技能报告,其中探讨了一些对理解开发人员环境至关重要的事情,本文将摘录编程语言排行部分 2018 开发者技能调查:不同行业中最热门的编程语言 尽管新语言经常 ...
- Unity3D手游开发日记(4) - 适合移动平台的热浪扭曲
热浪扭曲效果的实现,分两部分,一是抓图,二是扭曲扰动.其中难点在于抓图的处理,网上的解决方案有两种,在移动平台都有很多问题,只好自己实现了一种新的方案,效果还不错. 网上方案1. 用GrabPass抓 ...
- ZJOI2018 D1
归途的车上满是悲伤的气息 虽然早就预言到D1会滚粗,但一切都结束之后还是特别难过. 延时15min 50min T1 30pts 1.5h T2 10pts 2.5h T1 50pts 4.5h T3 ...
- 洛谷 P4066 [SHOI2003]吃豆豆 解题报告
P4066 [SHOI2003]吃豆豆 题目描述 两个PACMAN吃豆豆.一开始的时候,PACMAN都在坐标原点的左下方,豆豆都在右上方.PACMAN走到豆豆处就会吃掉它.PACMAN行走的路线很奇怪 ...
- codeforces765F Souvenirs
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- Redis的String、Hash类型命令
String是最简单的类型,一个Key对应一个Value,string类型是二进制安全的.Redis的string可以包含任何数据,比如jpg图片或者序列化的对象.最大上限是1G字节. Hash ...
- hadoop(三)HDFS基础使用
一.HDFS前言 1. 设计思想 分而治之:将大文件,大批量文件,分布式的存放于大量服务器上.以便于采取分而治之的方式对海量数据进行运算分析 2. 在大数据系统架构中的应用 ...