File tree Expand file tree Collapse file tree 1 file changed +17
-18
lines changed Expand file tree Collapse file tree 1 file changed +17
-18
lines changed Original file line number Diff line number Diff line change @@ -186,29 +186,28 @@ class Solution:
186
186
Go:
187
187
``` Go
188
188
func longestPalindromeSubseq (s string ) int {
189
- lenth := len (s)
190
- dp := make ([][]int ,lenth)
191
- for i := 0 ;i<lenth;i++{
192
- for j := 0 ;j<lenth;j++{
193
- if dp[i]==nil {
194
- dp[i]=make ([]int ,lenth)
195
- }
196
- if i==j{
197
- dp[i][j]=1
198
- }
189
+ size := len (s)
190
+ max := func (a, b int ) int {
191
+ if a > b {
192
+ return a
199
193
}
194
+ return b
200
195
}
201
- for i := lenth-1 ;i>=0 ;i--{
202
- for j := i+1 ;j<lenth;j++{
203
- if s[i]==s[j]{
204
- dp[i][j]=dp[i+1 ][j-1 ]+2
205
- }else {
206
- dp[i][j]=max (dp[i+1 ][j],dp[i][j-1 ])
196
+ dp := make ([][]int , size)
197
+ for i := 0 ; i < size; i++ {
198
+ dp[i] = make ([]int , size)
199
+ dp[i][i] = 1
200
+ }
201
+ for i := size - 1 ; i >= 0 ; i-- {
202
+ for j := i + 1 ; j < size; j++ {
203
+ if s[i] == s[j] {
204
+ dp[i][j] = dp[i+1 ][j-1 ] + 2
205
+ } else {
206
+ dp[i][j] = max (dp[i][j-1 ], dp[i+1 ][j])
207
207
}
208
208
}
209
209
}
210
-
211
- return dp[0 ][lenth-1 ]
210
+ return dp[0 ][size-1 ]
212
211
}
213
212
```
214
213
You can’t perform that action at this time.
0 commit comments