LeetCode Edit Distance(动态规划)
题意:给出两个字符串s1,s2, 问从s2变为s1需要几步操作。只支持3种操作:增加一个字符,删除一个字符,修改字符
思路:用f(i,j)表示字符串s1(0..i)和s2(0..j)转换需要的操作步数,
则有增加一个字符f(i,j) = f(i-1,j) + 1
删除一个字符f(i,j)=f(i, j-1) + 1
修改字符时,如果s1(i)==s2(j),有f(i,j)=f(i-1,j-1) ,否则f(i,j)=f(i-1,j-1)+1
具体代码如下:
public class Solution {public int minDistance(String word1, String word2) {int len1 = word1.length();int len2 = word2.length();int[][] dp = new int[len1 + 1][len2 + 1];for (int i = 1; i <= len2; i++){dp[0][i] = i;}for (int i = 1; i <= len1; i++){dp[i][0] = i;}for (int i = 1; i <= len1; i++) {for (int j = 1; j <= len2; j++) {int val = word1.charAt(i - 1) == word2.charAt(j - 1) ? 0 : 1;dp[i][j] = Math.min(dp[i - 1][j] + 1, Math.min(dp[i][j - 1] + 1, dp[i - 1][j - 1] + val));}}return dp[len1][len2];}
}
LeetCode Edit Distance(动态规划)相关推荐
- leetcode Edit Distance
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4051082.html 题目链接:leetcode Edit Distance 最短编辑距离, ...
- [leetcode]Edit Distance
先给一个例子,两个字符串eeba和abca相似度是多少呢,edit distance是一个很好的度量,定义从字符串a变到字符串b,所需要的最少的操作步骤(插入,删除,更改)为两个字符串之间的编辑距离. ...
- Leetcode72 edit Distance动态规划
题目描述: Given two words word1 and word2, find the minimum number of operations required to convert wor ...
- LeetCode(72)Edit Distance
题目 Given two words word1 and word2, find the minimum number of steps required to convert word1 to wo ...
- 【重点!记忆化递归+DP】LeetCode 72. Edit Distance
LeetCode 72. Edit Distance 参考链接:https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-72-edit ...
- [LeetCode] One Edit Distance 一个编辑距离
Given two strings S and T, determine if they are both one edit distance apart. 这道题是之前那道Edit Distance ...
- 详解编辑距离(Edit Distance)及其代码实现
概述 编辑距离(Minimum Edit Distance,MED),由俄罗斯科学家 Vladimir Levenshtein 在1965年提出,也因此而得名 Levenshtein Distance ...
- mysql 编辑距离 搜索_最小编辑距离(Minimum Edit Distance)
最小编辑距离 1)定义 编辑距离(Minimum Edit Distance,MED),又称Levenshtein距离,是指两个字符串之间,由一个转成另一个所需要的最少编辑操作次数.允许的编辑操作包括 ...
- python 编辑距离_python实现编辑距离edit distance
1.定义理解 edit distance--指两个字符串之间,一个转为另一个的最小编辑次数(方式有:插入/删除/替换) 若edit distance越小,则字符串之间的相似度越高. 例1: 输入: w ...
最新文章
- PHP5 $this self parent static的区别
- 感恩节QAD带你“吃鸡”
- STL二级配置器allocate的解析
- Intel Realsense D435 Post-processing filters 后处理过滤器(用于消除图像的黑洞)
- nginx配置ajax请求跨域
- 本人对于netty框架的一些理解,怎么与网站上的websock建立连接
- linux bash tutorial
- 网络爬虫--24.【selenium实战】实现拉勾网爬虫之--分析接口获取数据
- CSS3-变换-过渡-动画
- 用WebCollector制作一个爬取《知乎》并进行问题精准抽取的爬虫(JAVA)
- 2019.01.02 bzoj3513: [MUTC2013]idiots(fft)
- python 处理xml pandas_在python中解析xml到pandas数据帧
- Java书籍推荐(这些书你看过几本?)
- 105个上榜!2020年工业互联网试点示范项目名单公布
- Spring 整体结构
- 本机ip与外网ip(附带查询方法)
- 数据分析模型 第九章
- 个人空间岁末大回报活动12月30日获奖名单
- VUE React Angular
- 01-【istio】-【服务网格概述】服务网格的优势
热门文章
- Linux环境下获取网卡连接状态
- Best jQuery Plugins of 2010
- ceph 查看是否在恢复_Ceph的最佳实践
- python在哪里写代码比较适合-AI辅助写代码,Python之父都爱不释手的工具
- python3.7.2教程-python 3.7极速入门教程2 Hello与变量
- python小项目-python 小项目
- python编程 语言-Python成为2018年度编程语言,遥遥领先于其他语言
- python怎么画简单图片-小白艰难的Python图像的绘制
- php和python哪个好-PHP和Python语言选择哪个好
- 自学python爬虫要多久-入门Python爬虫要学习多久?