Update 0583.两个字符串的删除操作.md

添加 Go 版本
This commit is contained in:
wjjiang
2021-08-31 11:40:29 +08:00
committed by GitHub
parent 513ccbc51b
commit d1815018ec

View File

@ -147,8 +147,38 @@ class Solution:
```
Go
```go
func minDistance(word1 string, word2 string) int {
dp := make([][]int, len(word1)+1)
for i := 0; i < len(dp); i++ {
dp[i] = make([]int, len(word2)+1)
}
//初始化
for i := 0; i < len(dp); i++ {
dp[i][0] = i
}
for j := 0; j < len(dp[0]); j++ {
dp[0][j] = j
}
for i := 1; i < len(dp); i++ {
for j := 1; j < len(dp[i]); j++ {
if word1[i-1] == word2[j-1] {
dp[i][j] = dp[i-1][j-1]
} else {
dp[i][j] = min(min(dp[i-1][j]+1, dp[i][j-1]+1), dp[i-1][j-1]+2)
}
}
}
return dp[len(dp)-1][len(dp[0])-1]
}
func min(a, b int) int {
if a < b {
return a
}
return b
}
```
Javascript
```javascript
const minDistance = (word1, word2) => {