Skip to content

Commit

Permalink
添加0132.分割回文串II的Golang版本
Browse files Browse the repository at this point in the history
  • Loading branch information
Falldio authored Apr 7, 2023
1 parent 2ff490e commit 4b0982e
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions problems/0132.分割回文串II.md
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,45 @@ class Solution:
## Go

```go
func minCut(s string) int {
isValid := make([][]bool, len(s))
for i := 0; i < len(isValid); i++ {
isValid[i] = make([]bool, len(s))
isValid[i][i] = true
}
for i := len(s) - 1; i >= 0; i-- {
for j := i + 1; j < len(s); j++ {
if s[i] == s[j] && (isValid[i + 1][j - 1] || j - i == 1) {
isValid[i][j] = true
}
}
}

dp := make([]int, len(s))
for i := 0; i < len(s); i++ {
dp[i] = math.MaxInt
}
for i := 0; i < len(s); i++ {
if isValid[0][i] {
dp[i] = 0
continue
}
for j := 0; j < i; j++ {
if isValid[j + 1][i] {
dp[i] = min(dp[i], dp[j] + 1)
}
}
}
return dp[len(s) - 1]
}

func min(i, j int) int {
if i < j {
return i
} else {
return j
}
}
```

## JavaScript
Expand Down

0 comments on commit 4b0982e

Please sign in to comment.