Skip to content

Commit 089696b

Browse files
committed
[E:57/533, M:54/974, H:6/387] add No: 14 Longest Common Prefix
1 parent 274b92b commit 089696b

File tree

19 files changed

+559
-6
lines changed

19 files changed

+559
-6
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ leetcode 本地化题库,可本地刷题/测试/调试
1818
- 常用刷题 go 语言工具包 (utils 目录)
1919

2020
## 须知
21-
- 抱着怀疑的心态使用测试用例(当然每个测试用例都经过精心测试,难免会有意外
21+
- 抱着怀疑的心态使用测试用例(当然每个测试用例都经过精心测试,但难免会有意外
2222
- 每次执行测试时会先运行一次,检测是否会超时,超时默认为2s
23-
- 不可重复执行的算法,需要注意超时检测这次额外的调用是否影响结果
23+
- 当前所有测试用例共用一个运行环境,需考虑算法是否会对此有特殊要求(也就是说,你写的算法块最好是无状态的,注意:全局变量会在每个测试用例间共享)
24+
- 开始你的算法之旅
2425

2526
## 主要功能
2627
### 本地化测试

leet/golang.go

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ func (th SaveOption) golang() (err error) {
2020
if !ok {
2121
code = `
2222
func Export() {
23+
defer func() {
24+
if r := recover(); r != nil {
25+
fmt.Println("Panic:", r)
26+
fmt.Println()
27+
debug.PrintStack()
28+
os.Exit(0)
29+
}
30+
}()
2331
2432
}`
2533
}
@@ -94,8 +102,9 @@ func main() {
94102
95103
for idx, test := range tests {
96104
// 超时检测
105+
got := test.want
97106
timeout := leet.Timeout(timeoutDuration, func(ctx context.Context, cancel context.CancelFunc) {
98-
solution.Export(test.input)
107+
got = solution.Export(test.input)
99108
cancel()
100109
})
101110
@@ -104,7 +113,6 @@ func main() {
104113
continue
105114
}
106115
107-
got := solution.Export(test.input)
108116
if !reflect.DeepEqual(test.want, got) {
109117
testLog.Fail(idx+1, test.name, fmt.Sprintf(%s))
110118
continue
@@ -176,7 +184,27 @@ func parseGoCode(code string) (newCode string, ok bool) {
176184
exportFunction = fmt.Sprintf(`return %s`, exportFunction)
177185
}
178186

179-
code = structCode + code[:leftCurly+2] + "\t" + exportFunction + code[leftCurly+2:]
187+
recoverFunc := fmt.Sprintf(`defer func() {
188+
if r := recover(); r != nil {
189+
fmt.Println("Params: ", %s)
190+
fmt.Println("Panic:", r)
191+
fmt.Println()
192+
debug.PrintStack()
193+
os.Exit(0)
194+
}
195+
}()
196+
197+
`, strings.Join(params, ", "))
198+
199+
importCode := `import (
200+
"fmt"
201+
"os"
202+
"runtime/debug"
203+
)
204+
205+
`
206+
207+
code = importCode + structCode + code[:leftCurly+2] + recoverFunc + "\t" + exportFunction + code[leftCurly+2:]
180208

181209
newCode = strings.Replace(code, funcName, "Export", 1)
182210
return

questions/serial/简单/14/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
## [最长公共前缀](https://leetcode-cn.com/problems/longest-common-prefix/)
2+
3+
编写一个函数来查找字符串数组中的最长公共前缀。
4+
5+
如果不存在公共前缀,返回空字符串 `""`
6+
7+
**示例 1:**
8+
9+
`**输入:** ["flower","flow","flight"]
10+
**输出:** "fl"
11+
`
12+
13+
**示例 2:**
14+
15+
`**输入:** ["dog","racecar","car"]
16+
**输出:** ""
17+
**解释:** 输入不存在公共前缀。
18+
`
19+
20+
**说明:**
21+
22+
所有输入只包含小写字母 `a-z` 。
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"reflect"
7+
"time"
8+
9+
"github.com/gladmo/leetcode/leet"
10+
"github.com/gladmo/leetcode/questions/serial/简单/14/golang/solution"
11+
)
12+
13+
func main() {
14+
15+
tests := []struct {
16+
name string
17+
input []string
18+
want string
19+
}{
20+
{
21+
name: `test-[]`,
22+
input: []string{},
23+
want: "",
24+
},
25+
{
26+
name: `test-["haha"]`,
27+
input: []string{"haha"},
28+
want: "haha",
29+
},
30+
{
31+
name: `test-["ab", "a"]`,
32+
input: []string{"ab", "a"},
33+
want: "a",
34+
},
35+
{
36+
name: `test-["flower","flow","flight"]`,
37+
input: []string{"flower", "flow", "flight"},
38+
want: "fl",
39+
},
40+
{
41+
name: `test-["dog","racecar","car"]`,
42+
input: []string{"dog", "racecar", "car"},
43+
want: "",
44+
},
45+
}
46+
47+
testLog := leet.NewTestLog(len(tests))
48+
defer testLog.Render()
49+
50+
timeoutDuration := time.Second * 2
51+
52+
for idx, test := range tests {
53+
// 超时检测
54+
got := test.want
55+
timeout := leet.Timeout(timeoutDuration, func(ctx context.Context, cancel context.CancelFunc) {
56+
got = solution.Export(test.input)
57+
cancel()
58+
})
59+
60+
if timeout {
61+
testLog.Fail(idx+1, test.name, "timeout")
62+
continue
63+
}
64+
65+
if !reflect.DeepEqual(test.want, got) {
66+
testLog.Fail(idx+1, test.name, fmt.Sprintf("want: %v, got %v.", test.want, got))
67+
continue
68+
}
69+
70+
testLog.Pass(idx+1, test.name)
71+
}
72+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package solution
2+
3+
func Export(strs []string) string {
4+
return longestCommonPrefix(strs)
5+
}
6+
7+
/****************************************************/
8+
/******** 以下为 Leetcode 示例部分(提交PR请还原) *******/
9+
/******** 使用 (./leetcode clear) 初始化所有问题 *******/
10+
/****************************************************/
11+
12+
func longestCommonPrefix(strs []string) string {
13+
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package solution
2+
3+
func Export(strs []string) string {
4+
return longestCommonPrefix(strs)
5+
}
6+
7+
/****************************************************/
8+
/******** 以下为 Leetcode 示例部分(提交PR请还原) *******/
9+
/******** 使用 (./leetcode clear) 初始化所有问题 *******/
10+
/****************************************************/
11+
12+
func longestCommonPrefix(strs []string) string {
13+
14+
}

questions/store.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.
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+
"reflect"
7+
"time"
8+
9+
"github.com/gladmo/leetcode/questions/tags/动态规划/中等/longest-palindromic-substring/golang/solution"
10+
"github.com/gladmo/leetcode/leet"
11+
)
12+
13+
func main() {
14+
/*
15+
16+
"babad"
17+
18+
*/
19+
20+
tests := []struct {
21+
name string
22+
input [][]int
23+
want bool
24+
}{
25+
{
26+
name: "test-[[1],[2],[3],[]]",
27+
input: [][]int{
28+
{1},
29+
{2},
30+
{3},
31+
{},
32+
},
33+
want: true,
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 := 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 !reflect.DeepEqual(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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package solution
2+
3+
4+
import (
5+
"fmt"
6+
"os"
7+
"runtime/debug"
8+
)
9+
10+
func Export(s string) string {
11+
12+
defer func() {
13+
if r := recover(); r != nil {
14+
fmt.Println("Params: ", s)
15+
fmt.Println("Panic:", r)
16+
fmt.Println()
17+
debug.PrintStack()
18+
os.Exit(0)
19+
}
20+
}()
21+
return longestPalindrome(s)
22+
}
23+
24+
/****************************************************/
25+
/******** 以下为 Leetcode 示例部分(提交PR请还原) *******/
26+
/******** 使用 (./leetcode clear) 初始化所有问题 *******/
27+
/****************************************************/
28+
29+
func longestPalindrome(s string) string {
30+
31+
}

0 commit comments

Comments
 (0)