mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
Update 0583.两个字符串的删除操作.md
添加 Go 版本
This commit is contained in:
@ -147,8 +147,38 @@ class Solution:
|
|||||||
```
|
```
|
||||||
|
|
||||||
Go:
|
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:
|
||||||
```javascript
|
```javascript
|
||||||
const minDistance = (word1, word2) => {
|
const minDistance = (word1, word2) => {
|
||||||
|
Reference in New Issue
Block a user