Skip to content

Commit

Permalink
增加编辑距离解法代码
Browse files Browse the repository at this point in the history
  • Loading branch information
Jasper-Joe committed Jun 21, 2020
1 parent e364525 commit d6ab9bb
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion 动态规划系列/编辑距离.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit d6ab9bb

Please sign in to comment.