Skip to content

Commit

Permalink
Merge pull request youngyangyang04#2014 from Falldio/master
Browse files Browse the repository at this point in the history
添加0132.分割回文串II的Golang版本
  • Loading branch information
youngyangyang04 authored Apr 20, 2023
2 parents 1bbaa8e + 4b0982e commit f848b4f
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 f848b4f

Please sign in to comment.