[Leetcode 72]编辑距离 Edit Distance
【题目】
Given two words word1 and word2, find the minimum number of operations required to convert word1 to word2.
You have the following 3 operations permitted on a word:
- Insert a character
- Delete a character
- Replace a character
Example 1:
Input: word1 = "horse", word2 = "ros"
Output: 3
Explanation:
horse -> rorse (replace 'h' with 'r')
rorse -> rose (remove 'r')
rose -> ros (remove 'e')
把单词1变成单词2,可以修改/删除/插入,最少需要几步
【思路】
动态规划dp[i][j],i为原数据,j为现数据
三种情况plus、del、rep,求min+1(步骤)
【代码】
class Solution {
public int minDistance(String word1, String word2) {
int m=word1.length();
int n=word2.length();
int cost[][]=new int[m+1][n+1];
for(int i=0;i<m;i++){
cost[i][0]=i;}
for(int j=0;j<n;j++){
cost[0][j]=j;}
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(word1.charAt(i)==word2.charAt(j)){
cost[i+1][j+1]=cost[i][j];}
else{
int plus=cost[i+1][j];
int del=cost[i][j+1];
int rep=cost[i][j];
cost[i+1][j+1]=Math.min(plus,Math.min(del,rep))+1;
}
}
}
return cost[m][n];
}
}
[Leetcode 72]编辑距离 Edit Distance的更多相关文章
- leetcode@ [72/115] Edit Distance & Distinct Subsequences (Dynamic Programming)
https://leetcode.com/problems/edit-distance/ Given two words word1 and word2, find the minimum numbe ...
- (LeetCode 72)Edit Distance
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- 利用编辑距离(Edit Distance)计算两个字符串的相似度
利用编辑距离(Edit Distance)计算两个字符串的相似度 编辑距离(Edit Distance),又称Levenshtein距离,是指两个字串之间,由一个转成另一个所需的最少编辑操作次数.许可 ...
- [LeetCode] 161. One Edit Distance 一个编辑距离
Given two strings s and t, determine if they are both one edit distance apart. Note: There are 3 pos ...
- LeetCode(72) Edit Distance
题目 Given two words word1 and word2, find the minimum number of steps required to convert word1 to wo ...
- [leetcode]161. One Edit Distance编辑步数为一
Given two strings s and t, determine if they are both one edit distance apart. Note: There are 3 pos ...
- 编辑距离——Edit Distance
编辑距离 在计算机科学中,编辑距离是一种量化两个字符串差异程度的方法,也就是计算从一个字符串转换成另外一个字符串所需要的最少操作步骤.不同的编辑距离中定义了不同操作的集合.比较常用的莱温斯坦距离(Le ...
- [leetcode] 72. 编辑距离(二维动态规划)
72. 编辑距离 再次验证leetcode的评判机有问题啊!同样的代码,第一次提交超时,第二次提交就通过了! 此题用动态规划解决. 这题一开始还真难到我了,琢磨半天没有思路.于是乎去了网上喵了下题解看 ...
- ✡ leetcode 161. One Edit Distance 判断两个字符串是否是一步变换 --------- java
Given two strings S and T, determine if they are both one edit distance apart. 给定两个字符串,判断他们是否是一步变换得到 ...
随机推荐
- centos7 升级内核
rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org rpm -Uvh http://www.elrepo.org/elrepo-rel ...
- Spring 学习——Spring AOP——AOP配置篇Advice(有参数传递)
声明通知Advice 配置方式(以前置通知为例子) 方式一 <aop:config> <aop:aspect id="ikAspectAop" ref=" ...
- 工作流Activiti入门教程
https://blog.csdn.net/chenweifu365/article/details/79032758/
- python笔记--异常处理
Python异常处理 常见异常 AttributeError:属性错误,特性引用和赋值失败时会引发属性错误 NameError:试图访问的变量名不存在 SyntaxError:语法错误,代码形式错误 ...
- Multiple markers at this line - Missing semicolon时的解决方法
Myeclipse的web项目中的js文件报Multiple markers at this line - Missing semicolon时的解决方法 MyEclipse的web项目中的js文件报 ...
- Testlink与MantisBT集成
Testlink与MantisBT集成 关于两者集成的文章网上有很多,但是有些文章可能是作者写的时候自己不理解或有纰漏,有些文章写得是不够详细导致在配置中遗漏什么导致不成功.经过一天的不停尝试,终于完 ...
- spring+springmvc+hibernate 整合
三大框架反反复复搭了很多次,虽然每次都能搭起来,但是效率不高.最近重新搭了一次,理顺了思路,整理了需要注意的地方,分享出来. 工具:Eclipse(jdk 1.7) spring和hibernate版 ...
- Hadoop之运行环境搭建
一.虚拟机环境准备 1.克隆虚拟机 2.修改克隆虚拟机静态IP 3.修改主机名 4.关闭防火墙 5.创建hadoop用户 6.配置hadoop用户具有root权限 7.在/opt 目录下创建文件夹 1 ...
- HTTP响应 状态码描述
- (23)socket多进程并发
# 对于服务器自己本身,一个程序只能绑定一个端口 # 同一个端口可以多个客户端来连接, # 只要server_ip+ server_port +client_ip + cilent_port 不一样, ...