Java解决LeetCode72题 Edit Distance
题目描述
地址 : https://leetcode.com/problems/edit-distance/description/

思路
- 使用
dp[i][j]用来表示word1的0~i-1、word2的0~j-1的最小编辑距离 - 我们可以知道边界情况:
dp[i][0] = i、dp[0][j] = j,代表从""变为dp[0~i-1]或dp[0][0~j-1]所需要的次数
同时对于两个字符串的子串,都能分为最后一个字符相等或者不等的情况:
- 如果
word1[i-1] == word2[j-1]:dp[i][j] = dp[i-1][j-1] - 如果
word1[i-1] != word2[j-1]:- 向word1插入:
dp[i][j] = dp[i][j-1] + 1 - 从word1删除:
dp[i][j] = dp[i-1][j] + 1 - 替换word1元素:
dp[i][j] = dp[i-1][j-1] + 1
- 向word1插入:
public int minDistance(String word1, String word2) {
int n = word1.length();
int m = word2.length();
int[][] dp = new int[n + 1][m + 1];
for (int i = 0; i < m + 1; i++) {
dp[0][i] = i;
}
for (int i = 0; i < n + 1; i++) {
dp[i][0] = i;
}
for (int i = 1; i < n + 1; i++) {
for (int j = 1; j < m + 1; j++) {
if (word1.charAt(i - 1) == word2.charAt(j - 1)) {
dp[i][j] = dp[i - 1][j - 1];
} else {
dp[i][j] = Math.min(Math.min(dp[i - 1][j], dp[i][j - 1]), dp[i - 1][j - 1]) + 1;
}
}
}
return dp[n][m];
}
Java解决LeetCode72题 Edit Distance的更多相关文章
- Java for LeetCode 072 Edit Distance【HARD】
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- [Swift]LeetCode72. 编辑距离 | Edit Distance
Given two words word1 and word2, find the minimum number of operations required to convert word1 to ...
- [UCSD白板题] Compute the Edit Distance Between Two Strings
Problem Introduction The edit distinct between two strings is the minimum number of insertions, dele ...
- ✡ leetcode 161. One Edit Distance 判断两个字符串是否是一步变换 --------- java
Given two strings S and T, determine if they are both one edit distance apart. 给定两个字符串,判断他们是否是一步变换得到 ...
- 动态规划小结 - 二维动态规划 - 时间复杂度 O(n*n)的棋盘型,题 [LeetCode] Minimum Path Sum,Unique Paths II,Edit Distance
引言 二维动态规划中最常见的是棋盘型二维动态规划. 即 func(i, j) 往往只和 func(i-1, j-1), func(i-1, j) 以及 func(i, j-1) 有关 这种情况下,时间 ...
- 刷题72. Edit Distance
一.题目说明 题目72. Edit Distance,计算将word1转换为word2最少需要的操作.操作包含:插入一个字符,删除一个字符,替换一个字符.本题难度为Hard! 二.我的解答 这个题目一 ...
- 72. Edit Distance
题目: Given two words word1 and word2, find the minimum number of steps required to convert word1 to w ...
- LeetCode One Edit Distance
原题链接在这里:https://leetcode.com/problems/one-edit-distance/ Given two strings S and T, determine if the ...
- Minimum edit distance(levenshtein distance)(最小编辑距离)初探
最小编辑距离的定义:编辑距离(Edit Distance),又称Levenshtein距离.是指两个字串之间,由一个转成还有一个所需的最少编辑操作次数.许可的编辑操作包含将一个字符替换成还有一个字符. ...
随机推荐
- Week2-作业1-part2.阅读与思考
第一章.概论 原文: 在成熟的航空工业中,一个飞机发动机从构思到最后运行,不知道经历过多少人.多少工序.多少流程.多少相关知识的验证.我们无法想象,某个商用型号的发动机在飞行时发现问题,最初的设计师会 ...
- 简单实现wc.exe软件基本功能
简单实现wc.exe软件基本功能 软件需求分析: 一.基本功能 支持 -c 统计字符数(char count) 支持 -w 统计单词数(word count) 支持 -l 统计总行数(line ...
- Scrum Meeting Beta - 6
Scrum Meeting Beta - 6 NewTeam 2017/12/5 地点:主南201 任务反馈 团队成员 完成任务 计划任务 安万贺 完成了离线状态本地存储的读取Issue #133Pu ...
- 使用docker-compose编排django、mysql实战
背景: 本萌最近在部署自己开发的项目的时候发现同一套代码上传到服务器上后,部分功能莫名其妙的有点问题,服务器的各项配置都没有做过变动,所以想把项目转战到docker. 奈何刚接触docker,很多地方 ...
- java异常处理的throw和throws的区别
1. 区别 throws是用来声明一个方法可能抛出的所有异常信息,throws是将异常声明但是不处理,而是将异常往上传,谁调用我就交给谁处理.而throw则是指抛出的一个具体的异常类型. 2.分别介绍 ...
- Tomcat 总体结构
一.Tomcat 总体结构 1.Server(服务器)是Tomcat构成的顶级构成元素,所有一切均包含在Server中,Server的实现类StandardServer可以包含一个到多个Service ...
- 动态生成lookup字段
var i: Integer;begin //ADOQuery已打开 //在数据集打开的情况下新增加一个字段 with Self.ADOQuery1 do begin TDataSe ...
- VS2012 Nuget 安装 AutoMapper时报错的解决方法
VS2012 在.net 4.0下安装AutoMapper时,会报以下错误: “AutoMapper”已拥有为“Standard.Library”定义的依赖项. 'AutoMapper' alread ...
- STM32CubeMX介绍、下载与安装
一.简介 STM32CubeMX是一个配置STM32代码的工具,它把很多东西封装的比较好,硬件抽象层.中间层.示例代码等.现在ST公司升级和维护的库主要就是STM32CubeMX的HAL库和标准外设库 ...
- 一条慢SQL引发的血案
直接切入正题吧: 通常来说,我们看到的慢查询一般还不致于导致挂站,顶多就是应用响应变慢不过这个恰好今天被我撞见了,一个慢查询把整个网站搞挂了先看看这个SQL张撒样子: # Query_time: 70 ...