【Leetcode】72 Edit Distance
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的更多相关文章
- 【LeetCode】72. Edit Distance 编辑距离(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 记忆化搜索 动态规划 日期 题目地址:http ...
- 【一天一道LeetCode】#72. Edit Distance
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...
- 【LeetCode】849. Maximize Distance to Closest Person 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】461. Hamming Distance 解题报告(java & python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 方法一:异或 + 字符串分割 方法二: ...
- 【LeetCode】821. Shortest Distance to a Character 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 过两遍数组 日期 题目地址:https://leet ...
- 【leetcode】461. Hamming Distance
problem 461. Hamming Distance solution1: 根据题意,所求汉明距离指的是两个数字的二进制对应位不同的个数.对应位异或操作为1的累积和. class Solutio ...
- 【LeetCode】汉明距离(Hamming Distance)
这道题是LeetCode里的第461道题. 题目描述: 两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目. 给出两个整数 x 和 y,计算它们之间的汉明距离. 注意: 0 ≤ x, ...
- 【LEETCODE】72、分割回文串 III 第1278题
package y2019.Algorithm.dynamicprogramming.hard; /** * @Auther: xiaof * @Date: 2019/12/11 08:59 * @D ...
- 【LeetCode】1182. Shortest Distance to Target Color 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典+二分查找 日期 题目地址:https://lee ...
随机推荐
- 【8086汇编-Day1】预备知识
菜鸟的8086汇编入门之旅,偶有错处恭请大佬们指正. Ⅰ· 闲说一下 我为什么学汇编?相对于晦涩难懂的01010101011010机器语言(高低电平变化驱动机器做出不同反应),汇编语言用更便于记忆和使 ...
- [agc011F]Train Service Planning-[线段树优化dp+神秘思考题]
Description 传送门 Solution 请围观lhx大佬的博客(大佬写的太好了我都没有写的动力了em) Code #include<iostream> #include<c ...
- [BZOJ4712]洪水-[树链剖分+线段树]
Description 小A走到一个山脚下,准备给自己造一个小屋.这时候,小A的朋友(op,又叫管理员)打开了创造模式,然后飞到山顶放了格水.于是小A面前出现了一个瀑布.作为平民的小A只好老实巴交地爬 ...
- virsh常用维护命令
virsh常用命令 一些常用命令参数 [root@kvm-server ~]# virsh --help #查看命令帮忙 [ro ...
- OpenStack入门篇(三)之KVM介绍及安装
1.什么是虚拟化? 虚拟化是云计算的基础.简单的说,虚拟化使得在一台物理的服务器上可以跑多台虚拟机,虚拟机共享物理机的 CPU.内存.IO 硬件资源,但逻辑上虚拟机之间是相互隔离的. 物理机我们一般称 ...
- Oracle dba权限下修改用户密码 授予用户权限 解锁用户
1.修改用户密码 alter user scott identified by 123 2.授予用户权限 grant connect,resource to scott 3.解锁用户 alter us ...
- pg执行计划
- Intellij IDEA破解激活
本文使用破解补丁激活Intellij IDEA 第一步:下载破解补丁(http://idea.lanyus.com/) 第二步:将下载好的补丁放置磁盘的任意位置,下面是我放置的位置 第三步:打开Int ...
- python爬虫之requests库
在python爬虫中,要想获取url的原网页,就要用到众所周知的强大好用的requests库,在2018年python文档年度总结中,requests库使用率排行第一,接下来就开始简单的使用reque ...
- [转]git学习------>git-rev-parse命令初识
git学习------>git-rev-parse命令初识 2017年06月13日 10:04:13 阅读数:2172 一.准备工作 第一步:在d盘git test目录下,新建工作区根目录dem ...