Skip to content

Commit b0d2e44

Browse files
committed
[E:57/533, M:55/974, H:6/387] add No: 5 Longest Palindromic Substring
1 parent 089696b commit b0d2e44

File tree

11 files changed

+184
-47
lines changed

11 files changed

+184
-47
lines changed

leet/golang.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,10 @@ func parseGoCode(code string) (newCode string, ok bool) {
204204
205205
`
206206

207-
code = importCode + structCode + code[:leftCurly+2] + recoverFunc + "\t" + exportFunction + code[leftCurly+2:]
207+
code = importCode + structCode + code[:leftCurly+2] +
208+
"\t" + recoverFunc +
209+
"\t" + exportFunction +
210+
code[leftCurly+2:]
208211

209212
newCode = strings.Replace(code, funcName, "Export", 1)
210213
return
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
## [最长回文子串](https://leetcode-cn.com/problems/longest-palindromic-substring/)
2+
3+
给定一个字符串 `s`,找到 `s` 中最长的回文子串。你可以假设 `s` 的最大长度为 1000。
4+
5+
**示例 1:**
6+
7+
`**输入:** "babad"
8+
**输出:** "bab"
9+
**注意:** "aba" 也是一个有效答案。
10+
`
11+
12+
**示例 2:**
13+
14+
`**输入:** "cbbd"
15+
**输出:** "bb"
16+
`
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"strings"
7+
"time"
8+
9+
"github.com/gladmo/leetcode/leet"
10+
"github.com/gladmo/leetcode/questions/serial/中等/5/golang/solution"
11+
)
12+
13+
func main() {
14+
/*
15+
16+
"babad"
17+
18+
*/
19+
20+
tests := []struct {
21+
name string
22+
input string
23+
want []string
24+
}{
25+
{
26+
name: "test-babad",
27+
input: "babad",
28+
want: []string{"aba", "bab"},
29+
},
30+
{
31+
name: "test-cbbd",
32+
input: "cbbd",
33+
want: []string{"bb"},
34+
},
35+
}
36+
37+
testLog := leet.NewTestLog(len(tests))
38+
defer testLog.Render()
39+
40+
timeoutDuration := time.Second * 2
41+
42+
for idx, test := range tests {
43+
// 超时检测
44+
got := strings.Join(test.want, ",")
45+
timeout := leet.Timeout(timeoutDuration, func(ctx context.Context, cancel context.CancelFunc) {
46+
got = solution.Export(test.input)
47+
cancel()
48+
})
49+
50+
if timeout {
51+
testLog.Fail(idx+1, test.name, "timeout")
52+
continue
53+
}
54+
55+
if !strings.Contains(strings.Join(test.want, ","), got) {
56+
testLog.Fail(idx+1, test.name, fmt.Sprintf("want: %v, got %v.", test.want, got))
57+
continue
58+
}
59+
60+
testLog.Pass(idx+1, test.name)
61+
}
62+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package solution
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"runtime/debug"
7+
)
8+
9+
func Export(s string) string {
10+
defer func() {
11+
if r := recover(); r != nil {
12+
fmt.Println("Params: ", s)
13+
fmt.Println("Panic:", r)
14+
fmt.Println()
15+
debug.PrintStack()
16+
os.Exit(0)
17+
}
18+
}()
19+
20+
return longestPalindrome(s)
21+
}
22+
23+
/****************************************************/
24+
/******** 以下为 Leetcode 示例部分(提交PR请还原) *******/
25+
/******** 使用 (./leetcode clear) 初始化所有问题 *******/
26+
/****************************************************/
27+
28+
func longestPalindrome(s string) string {
29+
30+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package solution
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"runtime/debug"
7+
)
8+
9+
func Export(s string) string {
10+
defer func() {
11+
if r := recover(); r != nil {
12+
fmt.Println("Params: ", s)
13+
fmt.Println("Panic:", r)
14+
fmt.Println()
15+
debug.PrintStack()
16+
os.Exit(0)
17+
}
18+
}()
19+
20+
return longestPalindrome(s)
21+
}
22+
23+
/****************************************************/
24+
/******** 以下为 Leetcode 示例部分(提交PR请还原) *******/
25+
/******** 使用 (./leetcode clear) 初始化所有问题 *******/
26+
/****************************************************/
27+
28+
func longestPalindrome(s string) string {
29+
30+
}

questions/tags/动态规划/中等/longest-palindromic-substring/golang/main.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,34 @@ package main
33
import (
44
"context"
55
"fmt"
6-
"reflect"
6+
"strings"
77
"time"
88

9-
"github.com/gladmo/leetcode/questions/tags/动态规划/中等/longest-palindromic-substring/golang/solution"
109
"github.com/gladmo/leetcode/leet"
10+
"github.com/gladmo/leetcode/questions/serial/中等/5/golang/solution"
1111
)
1212

1313
func main() {
1414
/*
15-
16-
"babad"
1715
18-
*/
16+
"babad"
17+
18+
*/
1919

2020
tests := []struct {
2121
name string
22-
input [][]int
23-
want bool
22+
input string
23+
want []string
2424
}{
2525
{
26-
name: "test-[[1],[2],[3],[]]",
27-
input: [][]int{
28-
{1},
29-
{2},
30-
{3},
31-
{},
32-
},
33-
want: true,
26+
name: "test-babad",
27+
input: "babad",
28+
want: []string{"aba", "bab"},
29+
},
30+
{
31+
name: "test-cbbd",
32+
input: "cbbd",
33+
want: []string{"bb"},
3434
},
3535
}
3636

@@ -41,7 +41,7 @@ func main() {
4141

4242
for idx, test := range tests {
4343
// 超时检测
44-
got := test.want
44+
got := strings.Join(test.want, ",")
4545
timeout := leet.Timeout(timeoutDuration, func(ctx context.Context, cancel context.CancelFunc) {
4646
got = solution.Export(test.input)
4747
cancel()
@@ -52,7 +52,7 @@ func main() {
5252
continue
5353
}
5454

55-
if !reflect.DeepEqual(test.want, got) {
55+
if !strings.Contains(strings.Join(test.want, ","), got) {
5656
testLog.Fail(idx+1, test.name, fmt.Sprintf("want: %v, got %v.", test.want, got))
5757
continue
5858
}

questions/tags/动态规划/中等/longest-palindromic-substring/golang/solution/longest-palindromic-substring.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
package solution
22

3-
43
import (
54
"fmt"
65
"os"
76
"runtime/debug"
87
)
98

109
func Export(s string) string {
11-
1210
defer func() {
1311
if r := recover(); r != nil {
1412
fmt.Println("Params: ", s)
@@ -18,7 +16,8 @@ func Export(s string) string {
1816
os.Exit(0)
1917
}
2018
}()
21-
return longestPalindrome(s)
19+
20+
return longestPalindrome(s)
2221
}
2322

2423
/****************************************************/

questions/tags/动态规划/中等/longest-palindromic-substring/golang/solution/longest-palindromic-substring.go.clean

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
package solution
22

3-
43
import (
54
"fmt"
65
"os"
76
"runtime/debug"
87
)
98

109
func Export(s string) string {
11-
1210
defer func() {
1311
if r := recover(); r != nil {
1412
fmt.Println("Params: ", s)
@@ -18,7 +16,8 @@ func Export(s string) string {
1816
os.Exit(0)
1917
}
2018
}()
21-
return longestPalindrome(s)
19+
20+
return longestPalindrome(s)
2221
}
2322

2423
/****************************************************/

questions/tags/字符串/中等/longest-palindromic-substring/golang/main.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,34 @@ package main
33
import (
44
"context"
55
"fmt"
6-
"reflect"
6+
"strings"
77
"time"
88

9-
"github.com/gladmo/leetcode/questions/tags/字符串/中等/longest-palindromic-substring/golang/solution"
109
"github.com/gladmo/leetcode/leet"
10+
"github.com/gladmo/leetcode/questions/serial/中等/5/golang/solution"
1111
)
1212

1313
func main() {
1414
/*
15-
16-
"babad"
1715
18-
*/
16+
"babad"
17+
18+
*/
1919

2020
tests := []struct {
2121
name string
22-
input [][]int
23-
want bool
22+
input string
23+
want []string
2424
}{
2525
{
26-
name: "test-[[1],[2],[3],[]]",
27-
input: [][]int{
28-
{1},
29-
{2},
30-
{3},
31-
{},
32-
},
33-
want: true,
26+
name: "test-babad",
27+
input: "babad",
28+
want: []string{"aba", "bab"},
29+
},
30+
{
31+
name: "test-cbbd",
32+
input: "cbbd",
33+
want: []string{"bb"},
3434
},
3535
}
3636

@@ -41,7 +41,7 @@ func main() {
4141

4242
for idx, test := range tests {
4343
// 超时检测
44-
got := test.want
44+
got := strings.Join(test.want, ",")
4545
timeout := leet.Timeout(timeoutDuration, func(ctx context.Context, cancel context.CancelFunc) {
4646
got = solution.Export(test.input)
4747
cancel()
@@ -52,7 +52,7 @@ func main() {
5252
continue
5353
}
5454

55-
if !reflect.DeepEqual(test.want, got) {
55+
if !strings.Contains(strings.Join(test.want, ","), got) {
5656
testLog.Fail(idx+1, test.name, fmt.Sprintf("want: %v, got %v.", test.want, got))
5757
continue
5858
}

questions/tags/字符串/中等/longest-palindromic-substring/golang/solution/longest-palindromic-substring.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
package solution
22

3-
43
import (
54
"fmt"
65
"os"
76
"runtime/debug"
87
)
98

109
func Export(s string) string {
11-
1210
defer func() {
1311
if r := recover(); r != nil {
1412
fmt.Println("Params: ", s)
@@ -18,7 +16,8 @@ func Export(s string) string {
1816
os.Exit(0)
1917
}
2018
}()
21-
return longestPalindrome(s)
19+
20+
return longestPalindrome(s)
2221
}
2322

2423
/****************************************************/

0 commit comments

Comments
 (0)