提交 d6ab9bb5 编写于 作者: J jasper

增加编辑距离解法代码

上级 e364525b
......@@ -262,7 +262,38 @@ class Node {
![labuladong](../pictures/labuladong.png)
Jinglun Zhou 提供C++解法代码:
[labuladong](https://github.com/labuladong) 提供Java解法代码:
```
int minDistance(String s1, String s2) {
int m = s1.length(), n = s2.length();
int[][] dp = new int[m + 1][n + 1];
// base case
for (int i = 1; i <= m; i++)
dp[i][0] = i;
for (int j = 1; j <= n; j++)
dp[0][j] = j;
// 自底向上求解
for (int i = 1; i <= m; i++)
for (int j = 1; j <= n; j++)
if (s1.charAt(i-1) == s2.charAt(j-1))
dp[i][j] = dp[i - 1][j - 1];
else
dp[i][j] = min(
dp[i - 1][j] + 1,
dp[i][j - 1] + 1,
dp[i-1][j-1] + 1
);
// 储存着整个 s1 和 s2 的最小编辑距离
return dp[m][n];
}
int min(int a, int b, int c) {
return Math.min(a, Math.min(b, c));
}
```
[Jinglun Zhou](https://github.com/Jasper-Joe) 提供C++解法代码:
```CPP
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册