Skip to content

Commit c402021

Browse files
committed
5-最长回文子串
1 parent aca124a commit c402021

File tree

2 files changed

+37
-11
lines changed

2 files changed

+37
-11
lines changed

Diff for: 200-number-of-islands.go

+9-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package main
22

3-
import "fmt"
4-
53
func numIslands(grid [][]byte) int {
64
if grid == nil || len(grid) == 0 || len(grid[0]) == 0 {
75
return 0
@@ -33,12 +31,12 @@ func dfs(grid [][]byte, i, j int) {
3331
dfs(grid, i, j-1)
3432
}
3533

36-
func main() {
37-
grid := [][]byte{
38-
{'1', '1', '1', '1', '0'},
39-
{'1', '1', '0', '1', '0'},
40-
{'1', '1', '0', '0', '0'},
41-
{'0', '0', '0', '0', '0'},
42-
}
43-
fmt.Printf("%v \n", numIslands(grid))
44-
}
34+
//func main() {
35+
// grid := [][]byte{
36+
// {'1', '1', '1', '1', '0'},
37+
// {'1', '1', '0', '1', '0'},
38+
// {'1', '1', '0', '0', '0'},
39+
// {'0', '0', '0', '0', '0'},
40+
// }
41+
// fmt.Printf("%v \n", numIslands(grid))
42+
//}

Diff for: 5-longest-palindromic-substring.go

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package main
2+
3+
func longestPalindrome(s string) string {
4+
res := ""
5+
for i := 0; i < len(s); i++ {
6+
res = maxPalindrome(s, i, i, res)
7+
res = maxPalindrome(s, i, i+1, res)
8+
}
9+
return res
10+
}
11+
12+
func maxPalindrome(s string, i, j int, res string) string {
13+
sub := ""
14+
for i >= 0 && j < len(s) && s[i] == s[j] {
15+
sub = s[i : j+1]
16+
i--
17+
j++
18+
}
19+
if len(res) < len(sub) {
20+
return sub
21+
}
22+
return res
23+
}
24+
25+
//func main() {
26+
// s := "babad"
27+
// fmt.Printf("%v \n", longestPalindrome(s))
28+
//}

0 commit comments

Comments
 (0)