File tree 2 files changed +29
-0
lines changed
0005.Longest Palindromic Substring
2 files changed +29
-0
lines changed Original file line number Diff line number Diff line change
1
+ func longestPalindrome (s string ) string {
2
+ length := len (s )
3
+ if length == 0 {
4
+ return ""
5
+ }
6
+ res := make ([][]bool , length )
7
+ for i := 0 ; i < length ; i ++ {
8
+ res [i ] = make ([]bool , length )
9
+ }
10
+ max := 1
11
+ start := 0
12
+ for i := 0 ; i < length ; i ++ {
13
+ for j := 0 ; j <= i ; j ++ {
14
+ if i - j < 2 {
15
+ res [j ][i ] = s [j ] == s [i ]
16
+ } else {
17
+ res [j ][i ] = s [j ] == s [i ] && res [j + 1 ][i - 1 ]
18
+ }
19
+
20
+ if res [j ][i ] && max < i - j + 1 {
21
+ max = i - j + 1
22
+ start = j
23
+ }
24
+ }
25
+ }
26
+
27
+ return s [start :start + max ]
28
+ }
Original file line number Diff line number Diff line change 35
35
├── 0005.Longest Palindromic Substring
36
36
│ ├── README.md
37
37
│ ├── Solution.java
38
+ │ ├── Solution.go
38
39
│ └── Solution.js
39
40
├── 0006.ZigZag Conversion
40
41
│ ├── README.md
You can’t perform that action at this time.
0 commit comments