Skip to content

Commit

Permalink
Update 0072.编辑距离.md
Browse files Browse the repository at this point in the history
  • Loading branch information
QuinnDK authored May 14, 2021
1 parent cf42d80 commit 62f7d62
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion problems/0072.编辑距离.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,40 @@ Python:
Go:
```Go
func minDistance(word1 string, word2 string) int {
m, n := len(word1), len(word2)
dp := make([][]int, m+1)
for i := range dp {
dp[i] = make([]int, n+1)
}
for i := 0; i < m+1; i++ {
dp[i][0] = i // word1[i] 变成 word2[0], 删掉 word1[i], 需要 i 部操作
}
for j := 0; j < n+1; j++ {
dp[0][j] = j // word1[0] 变成 word2[j], 插入 word1[j],需要 j 部操作
}
for i := 1; i < m+1; i++ {
for j := 1; j < n+1; j++ {
if word1[i-1] == word2[j-1] {
dp[i][j] = dp[i-1][j-1]
} else { // Min(插入,删除,替换)
dp[i][j] = Min(dp[i][j-1], dp[i-1][j], dp[i-1][j-1]) + 1
}
}
}
return dp[m][n]
}
func Min(args ...int) int {
min := args[0]
for _, item := range args {
if item < min {
min = item
}
}
return min
}
```



Expand Down

0 comments on commit 62f7d62

Please sign in to comment.