72. Edit Distance

Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.)

You have the following 3 operations permitted on a word:

a) Insert a character
b) Delete a character
c) Replace a character

Tips: 本题是一道动态规划问题,到当前字符要移动步数的步数,有到前一个字符移动步数决定。

dp[i][j]表示从word1前i个字符转换到word2前j个字符最少的步骤数。

从word1转换为Word2,能进行的才操作包括插入、删除、与替换。

①插入:dp[i][j]等于包括字符i-1之前的所有字符转换为包括j字符之前的所有字符的步数加一。即

insert=dp[i-1][j]+1;

②删除:dp[i][j]等于包括字符i之前的所有字符转换为包括j-1字符之前的所有字符的步数加一。即

del=dp[i][j-1]+1;

③替换:dp[i][j]等于包括字符i-1之前的所有字符转换为包括j-1字符之前的所有字符的步数加一。即

change=dp[i-1][j-1]+1;

dp[i][j]最终的值应为以上三个数值的最小值。

当 word1与word2遇到一样的字符dp[i][j]=dp[i-1][j-1];

package hard;

public class L72EditDistance {

	public int minDistance(String word1, String word2) {
//dp二维数组表示从word1的第i个位置转换为word2的第j个位置需要的步数。
int len1=word1.length();
int len2=word2.length();
int[][]dp = new int[len1+1][len2+1];
int length=len1>len2?len1:len2;
for(int i=0;i<=len1;i++){
dp[i][0]=i;
}
for(int j=0;j<=len2;j++){
dp[0][j]=j;
}
for(int i=1;i<=len1;i++){
char ch1=word1.charAt(i-1);
for(int j=1;j<=len2;j++){
char ch2=word2.charAt(j-1);
//当前word1字符等于word2字符,则dp[i][j]=dp[i-1][j-1];
if(ch1==ch2){
dp[i][j]=dp[i-1][j-1];
}else{
int insert=dp[i-1][j]+1;
int del=dp[i][j-1]+1;
int change=dp[i-1][j-1]+1;
int min=Math.min(insert,del);
min=Math.min(min,change);
dp[i][j]=min;
} }
}
return dp[len1][len2];
}
public static void main(String[] args) {
String word1="hello";
String word2="hallo";
L72EditDistance l72=new L72EditDistance();
int count = l72.minDistance(word1, word2);
System.out.println(count); } }

【Leetcode】72 Edit Distance的更多相关文章

  1. 【LeetCode】72. Edit Distance 编辑距离(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 记忆化搜索 动态规划 日期 题目地址:http ...

  2. 【一天一道LeetCode】#72. Edit Distance

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...

  3. 【LeetCode】849. Maximize Distance to Closest Person 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  4. 【LeetCode】461. Hamming Distance 解题报告(java & python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 方法一:异或 + 字符串分割 方法二: ...

  5. 【LeetCode】821. Shortest Distance to a Character 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 过两遍数组 日期 题目地址:https://leet ...

  6. 【leetcode】461. Hamming Distance

    problem 461. Hamming Distance solution1: 根据题意,所求汉明距离指的是两个数字的二进制对应位不同的个数.对应位异或操作为1的累积和. class Solutio ...

  7. 【LeetCode】汉明距离(Hamming Distance)

    这道题是LeetCode里的第461道题. 题目描述: 两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目. 给出两个整数 x 和 y,计算它们之间的汉明距离. 注意: 0 ≤ x,  ...

  8. 【LEETCODE】72、分割回文串 III 第1278题

    package y2019.Algorithm.dynamicprogramming.hard; /** * @Auther: xiaof * @Date: 2019/12/11 08:59 * @D ...

  9. 【LeetCode】1182. Shortest Distance to Target Color 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典+二分查找 日期 题目地址:https://lee ...

随机推荐

  1. 二叉树 ADT接口 遍历算法 常规运算

    BTree.h   (结构定义, 基本操作, 遍历) #define MS 10 typedef struct BTreeNode{ char data; struct BTreeNode * lef ...

  2. Python之文件及文件系统

    open() 方法: Python open() 方法用于打开一个文件,并返回文件对象,在对文件进行处理过程都需要使用到这个函数,如果该文件无法被打开,会抛出 OSError. 注意:使用 open( ...

  3. 20155234 实验三 敏捷开发与XP实践

    20155234 实验三 敏捷开发与XP实践 实验内容 1.XP基础 2.XP核心实践 3.相关工具 实验步骤 (一)敏捷开发与XP 敏捷开发(Agile Development)是一种以人为核心.迭 ...

  4. WPF 任务栏颜色 - 简书

    原文:WPF 任务栏颜色 - 简书 先看看效果,这种效果可以用来做进度条或者消息通知闪烁.   image.png   image.png   image.png   image.png 有一个好消息 ...

  5. [arc065E]Manhattan Compass[曼哈顿距离和切比雪夫距离转换]

    Description 传送门 Solution 题目要求的是曼达顿距离,对于每个点(x,y),我们把它变为(x-y,x+y),就可以转换成求切比雪夫距离了. 证明如下:$max(\left | (x ...

  6. 【转载】图说C++对象模型:对象内存布局详解

    原文: 图说C++对象模型:对象内存布局详解 正文 回到顶部 0.前言 文章较长,而且内容相对来说比较枯燥,希望对C++对象的内存布局.虚表指针.虚基类指针等有深入了解的朋友可以慢慢看.本文的结论都在 ...

  7. kali2.0下JAVA安装

    参考网址:http://www.blackmoreops.com/2013/10/26/how-to-install-java-jdk-in-kali-linux/ 1.下载javase,http:/ ...

  8. python3.0 day02 列表、元组 、字典、字符串操作

    1.列表.元组操作 列表是我们最以后最常用的数据类型之一,通过列表可以对数据实现最方便的存储.修改等操作,类似于其他语言中的数组. 定义列表 names = ['Lioa',"Tenglan ...

  9. MQ配置安装

    一,MQ安装 ./mqlicense.sh -accept rpm -ivh MQSeries*.rpm --  rpm -qa|grep MQSeries 二,MQ配置 环境变量配置(MQM)实际安 ...

  10. Docker--从安装到搭建环境

    docker 1. ubuntu下安装docker 安装docker有两种方法: 一种是用官方的bash脚本一键安装. 直接一条命令就解决了: $ curl -sSL https://get.dock ...