【题目】

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. spring整合dubbo[单机版]

    Spring整合Dubbo,这个是用xml配置的 (方式一) 来梳理下步骤: 1. 安装zookeeper,在进行简单配置[这里使用单机模式,不用集群] 2. 创建maven项目,构建项目结构 3. ...

  2. jQuery初识、函数、对象

    初识jQuery 官方地址:http://jquery.com/ what:一个优秀的JS函数库(封装了BOM.DOM(主要)) why: HTML元素选取(选择器) HTML元素操作 CSS操作 H ...

  3. vue怎么引入外网json文件

    今日吃午饭时,伊万卡前端小妹问了我一个问题."App中有一个模块是用H5做的,其中有一个接口读取的是本地json资源文件,但是这个文件修改时间不定,我又不想每次修改了这个文件再重新发版打个包 ...

  4. Java NIO 系列学习(一)Java NIO概述

    参考资料: http://www.importnew.com/19816.html http://ifeve.com/overview/ NIO 三大核心部分: Channel(通道).Buffer( ...

  5. 用 jupyter notebook 打开 oui.txt 文件出现的问题及解决方案

    问题背景:下载了2018 IEEE 最新的 oui.txt 文件.里面包含了 设备 MAC 地址的前六位对应的厂商.要做的工作是,将海量设备的 MAC 地址与 oui.txt 文件的信息比对,统计出 ...

  6. R语言 重命名目录下所有文件

    myfilepath <- "F:/paper2/climateExposure/wjj_mec/second/paths/" setwd(myfilepath) allty ...

  7. 简单了解 DLL中, .def 文件及C#调用C++方法

    DLL中导出函数的声明有两种方式: 1.在函数声明中加上__declspec(dllexport) //以下内容为 .h 文件中的内容 //向外界提供的端口 extern"C" _ ...

  8. 8.4 GOF设计模式三: 外观模式 Facade

    GOF设计模式三: 外观模式 Facade  “现有系统”功能强大.复杂,开发“新系统”需要用到其中一部分,但又要增加一部 分新功能,该怎么办?4.1 Facade Pattern: Key Fea ...

  9. SWUST OJ(1102)

    顺序表上数据的划分问题的实现 #include <iostream> #include <cstdlib> using namespace std; int main() { ...

  10. Postman中x-www-form-urlencoded请求K-V的ajax实现

    在Postman中使用x-www-form-urlencoded,并且用K-V传值,但是在代码中用ajax来请求,传值一直有问题,静下心来思考才发现K-V传入的是string,所以记录下来以防忘记!! ...