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. 编译Libuv

    Libuv https://github.com/libuv/libuv LibSourcey是基于libuv,集合了第三方用于视频流的开源库,使用C++11. 下载最新 https://dist.l ...

  2. 笔记(assert 断言)

    并发:在同一个时间段交替执行多个任务并行:在同一个时间点同时执行多个任务串行:同时执行的多个任务按顺序执行(换句话说就是一个任务执行完后才能执行下一个任务) #mysql limit用法: selec ...

  3. go字符串转换

    package main import ( "fmt" "strconv" ) /* 常用总结 1.str 转 int a, _ := strconv.Atoi ...

  4. PL/SQL轻量版(三)——游标与异常处理

    一.游标 1.概念 游标是一个 指向上下文的句柄( handle) 或指针.通过游标,PL/SQL 可以控制上下文区和处理语句时上下文区会发生些什么事情. 2.游标处理 处理显式游标 主要包含以下四个 ...

  5. 20155226 实验四 Android开发基础

    20155226第四次实验报告 一.实验内容及步骤 Android Stuidio的安装测试: 安装 Android Stuidio 完成Hello World, 要求修改res目录中的内容,Hell ...

  6. 20155316 实验四 《Android程序设计》

    实验1 实验内容 Android Stuidio的安装测试: 参考<Java和Android开发学习指南(第二版)(EPUBIT,Java for Android 2nd)>第二十四章: ...

  7. 20155316 2016-2017-2 《Java程序设计》第9周学习总结

    教材学习内容总结 课堂学习内容 不要上帝类,设计小类 soild设计 开放封闭原则:对扩充开放,对修改封闭 OOD方案 DIP 基耦合 教材学习内容 JDBC架构 交易与隔离层级 RowSet .cl ...

  8. 百度地图api 常用 例子

    功能一:获取map地图窗口的可视区域: var map = new BMap.Map("allmap");            // 创建Map实例 map.centerAndZ ...

  9. c++ 时间函数和结构化数据

     time和localtime  数据结构概念  struct关键字  认识数据结构  自定义结构 例:获取当前系统日期和时间;(代码例子) 一.函数: time 函数time()返回的是当 ...

  10. FFT&NTT总结

    FFT&NTT总结 一些概念 \(DFT:\)离散傅里叶变换\(\rightarrow O(n^2)\)计算多项式卷积 \(FFT:\)快速傅里叶变换\(\rightarrow O(nlogn ...