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距离.是指两个字串之间,由一个转成还有一个所需的最少编辑操作次数.许可的编辑操作包含将一个字符替换成还有一个字符. ...
随机推荐
- ADT队列/FIFO表
队列是一种特殊的表,只在表首进行删除,只在表尾进行插入,按照先进先出原则操作(First In First Out),又称为FIFO表: 队列的指针实现代码: #include<cstdio&g ...
- 服务 在初始化安装时发生异常:System.IO.FileNotFoundException: 未能加载文件或******
这个问题是在用installutil.exe安装服务时候碰到的 解决方法就是把installutil.exe文件直接复制到要安装的目录下 installutil.exe的所在位置 windows/mi ...
- Linux下CPU信息的查看
逻辑CPU个数: cat /proc/cpuinfo | grep "processor" | wc -l //32 物理CPU个数: cat /proc/cpuinfo ...
- vue 组件 单选切换控制模板 v-bind-is
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>T ...
- spring 中 ThreadPoolTaskExecutor 的使用
配置文件代码如下: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="htt ...
- Codeforces 449B_Jzzhu and Cities
给一个无向图,外加一些特殊的连接原点的无向边.在不改变原点与所有点的最短路的情况下,最多可以删除多少条特殊边? 首先我们把所有的边夹杂在一起.spfa跑出与所有点的最短路. 接下来我们通过一次bfs来 ...
- 随机场(Random field)
一.随机场定义 http://zh.wikipedia.org/zh-cn/随机场 随机场(Random field)定义如下: 在概率论中, 由样本空间Ω = {0, 1, …, G − 1}n取样 ...
- 【AGC010F】Tree Game
Description 有一棵\(n\)个节点的树(\(n \le 3000\)),第\(i\)条边连接\(a_i,b_i\),每个节点\(i\)上有\(A_i\)个石子,高桥君和青木君将在树上玩游戏 ...
- 【科技】扩展Lucas随想
扩展Lucas解决的还是一个很Simple的问题: 求:$C_{n}^{m} \; mod \; p$. 其中$n,m$都会比较大,而$p$不是很大,而且不一定是质数. 扩展Lucas可以说和Luca ...
- UVA.11806 Cheerleaders (组合数学 容斥原理 二进制枚举)
UVA.11806 Cheerleaders (组合数学 容斥原理 二进制枚举) 题意分析 给出n*m的矩形格子,给出k个点,每个格子里面可以放一个点.现在要求格子的最外围一圈的每行每列,至少要放一个 ...