[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. 给定两个字符串,判断他们是否是一步变换得到 ...
随机推荐
- time模块和os模块,json模块
import time # def month(n): # time.local() # struct_time=time.strptime("%Y-%m-1","%Y- ...
- Android文件各种存储路径的比较
1.File cacheDir = context.getCacheDir(); 应用内部存储空间(数据文件私有)文件存储到这个路径下,不需要申请权限,当应用被卸载的时候,目录下的文件会被删除. 需要 ...
- scrapy 爬虫的暂停与重启
暂停爬虫项目 首先在项目目录下创建一个文件夹用来存放暂停爬虫时的待处理请求url以及其他的信息.(文件夹名称:job_info) 在启动爬虫项目时候用pycharm自带的终端启动输入下面的命令: sc ...
- 人生苦短,我用Python——博客目录
计算机基础 计算机硬件基础知识 操作系统基础 Python基础 Windows环境下Python2和Python3的安装 交互式环境与变量的使用 简单介绍Python基本数据类型及程序交互 基本运算符 ...
- 2019 年 GrapeCity Documents 产品路线图
前言 | 问题背景 随着软件行业引入新的硬件和操作系统,我们看到更多的托管框架与.NET技术保持同步.Microsoft的.NET Standard和.NET Core定义了一个跨平台规范,为应用程序 ...
- [转载]如何快速下载、安装和配置chromedriver ?
转自:https://jingyan.baidu.com/album/f7ff0bfcdd89ed2e27bb1379.html?picindex=7 下载地址: http://npm.taobao. ...
- Tensorflow训练和预测中的BN层的坑
以前使用Caffe的时候没注意这个,现在使用预训练模型来动手做时遇到了.在slim中的自带模型中inception, resnet, mobilenet等都自带BN层,这个坑在<实战Google ...
- 融云(API)
先记录下融云API地址:真是难找 IMKit: https://www.rongcloud.cn/docs/api/android/imkit/index.html IMLib: https://ww ...
- JDBC中execute、executeQuery和executeUpdate的区别
Statement 接口提供了三种执行 SQL 语句的方法:executeQuery.executeUpdate 和 execute.使用哪一个方法由 SQL 语句所产生的内容决定. 1>方法e ...
- js二分查找算法
二分查找高效的前提是数据结构是有序的.就好比猜1~100之间的数,先猜50,如果太大了就猜25,如果太小了就猜75.每一次都猜最大值和最小值的中间点. 1.随机生成100个0~100之间的随机数. v ...