【题目】

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:

  1. Insert a character
  2. Delete a character
  3. 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的更多相关文章

  1. 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 ...

  2. (LeetCode 72)Edit Distance

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

  3. 利用编辑距离(Edit Distance)计算两个字符串的相似度

    利用编辑距离(Edit Distance)计算两个字符串的相似度 编辑距离(Edit Distance),又称Levenshtein距离,是指两个字串之间,由一个转成另一个所需的最少编辑操作次数.许可 ...

  4. [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 ...

  5. LeetCode(72) Edit Distance

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

  6. [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 ...

  7. 编辑距离——Edit Distance

    编辑距离 在计算机科学中,编辑距离是一种量化两个字符串差异程度的方法,也就是计算从一个字符串转换成另外一个字符串所需要的最少操作步骤.不同的编辑距离中定义了不同操作的集合.比较常用的莱温斯坦距离(Le ...

  8. [leetcode] 72. 编辑距离(二维动态规划)

    72. 编辑距离 再次验证leetcode的评判机有问题啊!同样的代码,第一次提交超时,第二次提交就通过了! 此题用动态规划解决. 这题一开始还真难到我了,琢磨半天没有思路.于是乎去了网上喵了下题解看 ...

  9. ✡ leetcode 161. One Edit Distance 判断两个字符串是否是一步变换 --------- java

    Given two strings S and T, determine if they are both one edit distance apart. 给定两个字符串,判断他们是否是一步变换得到 ...

随机推荐

  1. 0x16 Tire

    参考链接:https://www.cnblogs.com/TheRoadToTheGold/p/6290732.html 题目链接:https://www.acwing.com/problem/con ...

  2. Synchronized使用方法

    Synchronized是我们常用来维持线程安全时使用的一个关键字,内部通过monitor(监视器锁,由C++实现)来实现.而monitor本质又是依赖底层操作系统的mutex lock来实现.而操作 ...

  3. Bootstrap3基础 栅格系统 1行最多12列

      内容 参数   OS   Windows 10 x64   browser   Firefox 65.0.2   framework     Bootstrap 3.3.7   editor    ...

  4. JS高程关于ajax的学习笔记

    1.ajax介绍 ajax技术可以实现浏览器向服务器请求数据时不需要重新加载页面,就可以从服务器中获取需要的数据. ajax技术的核心是XMLHttpRequest对象(简称XHR),XHR对象为向服 ...

  5. UVA11020 Efficient Solutions

    思路 先判断一个点能不能插入,能插入的话删除所有因为它而没有优势的点 注意 写 S.erase(it); it++; 会RE 要写 S.erase(it++); 代码 #include <cst ...

  6. wireshark基础学习—第一部分wireshark的基础知识

    1.Wireshark主窗口 Wireshark的主窗口如下所示 2.每个面板的内容 Packet List(数据包列表): 最上面的面板用表格显示了当前不惑文件中的所有数据包,其中包括了数据包序号. ...

  7. Linux环境——MySQL安装及配置(5.7版本)

    数据库安装包下载地址:https://dev.mysql.com/downloads/mysql/ 我的环境是Linux  Red Hat Enterprlse Linux (64位),本次安装的是M ...

  8. Codeforces 799 D. Field expansion

    题目链接:http://codeforces.com/contest/799/problem/D 因为${a_i>=2}$那么一个数字至多操作${log_{2}^{max(a,b)/min(h, ...

  9. 4 - BFS & Topological Algorithm

    615. Course Schedule https://www.lintcode.com/problem/course-schedule/description?_from=ladder&& ...

  10. 如何成为F1车手?

    sorry,玩了几天的GT sport才发现赛车有多难,理论的最佳走线是存在的,但是真实的赛道实在是千变万化,弯道形状角度.高低差.F1还有温度和风速,甚至是路面上的一个碎石都会极大地影响你的成绩.赛 ...