Skip to content

Commit 4b52444

Browse files
committed
[E:58/533, M:57/974, H:6/387] add No: 28 Implement strStr()
1 parent 1553afa commit 4b52444

File tree

14 files changed

+450
-2
lines changed

14 files changed

+450
-2
lines changed

questions/serial/中等/151/golang/solution/reverse-words-in-a-string.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"os"
66
"runtime/debug"
7+
"strings"
78
)
89

910
func Export(s string) string {
@@ -26,5 +27,5 @@ func Export(s string) string {
2627
/****************************************************/
2728

2829
func reverseWords(s string) string {
29-
30+
strings.Index()
3031
}

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
## [实现 strStr()](https://leetcode-cn.com/problems/implement-strstr/)
2+
3+
实现 [strStr()](https://baike.baidu.com/item/strstr/811469) 函数。
4+
5+
给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始)。如果不存在,则返回  **-1**
6+
7+
**示例 1:**
8+
9+
`**输入:** haystack = "hello", needle = "ll"
10+
**输出:** 2
11+
`
12+
13+
**示例 2:**
14+
15+
`**输入:** haystack = "aaaaa", needle = "bba"
16+
**输出:** -1
17+
`
18+
19+
**说明:**
20+
21+
当 `needle` 是空字符串时,我们应当返回什么值呢?这是一个在面试中很好的问题。
22+
23+
对于本题而言,当 `needle` 是空字符串时我们应当返回 0 。这与C语言的 [strstr()](https://baike.baidu.com/item/strstr/811469) 以及 Java的 [indexOf()](https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#indexOf(java.lang.String)) 定义相符。
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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/简单/28/golang/solution"
11+
)
12+
13+
func main() {
14+
/*
15+
16+
"hello"
17+
"ll"
18+
19+
*/
20+
21+
tests := []struct {
22+
name string
23+
input1 string
24+
input2 string
25+
want int
26+
}{
27+
{
28+
name: "test-hello-ll",
29+
input1: "hello",
30+
input2: "ll",
31+
want: 2,
32+
},
33+
{
34+
name: "test-aaaaa-bba",
35+
input1: "aaaaa",
36+
input2: "bba",
37+
want: -1,
38+
},
39+
}
40+
41+
testLog := leet.NewTestLog(len(tests))
42+
defer testLog.Render()
43+
44+
timeoutDuration := time.Second * 2
45+
46+
for idx, test := range tests {
47+
// 超时检测
48+
got := test.want
49+
timeout := leet.Timeout(timeoutDuration, func(ctx context.Context, cancel context.CancelFunc) {
50+
got = solution.Export(test.input1, test.input2)
51+
cancel()
52+
})
53+
54+
if timeout {
55+
testLog.Fail(idx+1, test.name, "timeout")
56+
continue
57+
}
58+
59+
if !reflect.DeepEqual(test.want, got) {
60+
testLog.Fail(idx+1, test.name, fmt.Sprintf("want: %v, got %v.", test.want, got))
61+
continue
62+
}
63+
64+
testLog.Pass(idx+1, test.name)
65+
}
66+
}
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(haystack string, needle string) int {
10+
defer func() {
11+
if r := recover(); r != nil {
12+
fmt.Println("Params: ", haystack, needle)
13+
fmt.Println("Panic:", r)
14+
fmt.Println()
15+
debug.PrintStack()
16+
os.Exit(0)
17+
}
18+
}()
19+
20+
return strStr(haystack, needle)
21+
}
22+
23+
/****************************************************/
24+
/******** 以下为 Leetcode 示例部分(提交PR请还原) *******/
25+
/******** 使用 (./leetcode clear) 初始化所有问题 *******/
26+
/****************************************************/
27+
28+
func strStr(haystack string, needle string) int {
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(haystack string, needle string) int {
10+
defer func() {
11+
if r := recover(); r != nil {
12+
fmt.Println("Params: ", haystack, needle)
13+
fmt.Println("Panic:", r)
14+
fmt.Println()
15+
debug.PrintStack()
16+
os.Exit(0)
17+
}
18+
}()
19+
20+
return strStr(haystack, needle)
21+
}
22+
23+
/****************************************************/
24+
/******** 以下为 Leetcode 示例部分(提交PR请还原) *******/
25+
/******** 使用 (./leetcode clear) 初始化所有问题 *******/
26+
/****************************************************/
27+
28+
func strStr(haystack string, needle string) int {
29+
30+
}

questions/store.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
## [实现 strStr()](https://leetcode-cn.com/problems/implement-strstr/)
2+
3+
实现 [strStr()](https://baike.baidu.com/item/strstr/811469) 函数。
4+
5+
给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始)。如果不存在,则返回  **-1**
6+
7+
**示例 1:**
8+
9+
`**输入:** haystack = "hello", needle = "ll"
10+
**输出:** 2
11+
`
12+
13+
**示例 2:**
14+
15+
`**输入:** haystack = "aaaaa", needle = "bba"
16+
**输出:** -1
17+
`
18+
19+
**说明:**
20+
21+
当 `needle` 是空字符串时,我们应当返回什么值呢?这是一个在面试中很好的问题。
22+
23+
对于本题而言,当 `needle` 是空字符串时我们应当返回 0 。这与C语言的 [strstr()](https://baike.baidu.com/item/strstr/811469) 以及 Java的 [indexOf()](https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#indexOf(java.lang.String)) 定义相符。
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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/简单/28/golang/solution"
11+
)
12+
13+
func main() {
14+
/*
15+
16+
"hello"
17+
"ll"
18+
19+
*/
20+
21+
tests := []struct {
22+
name string
23+
input1 string
24+
input2 string
25+
want int
26+
}{
27+
{
28+
name: "test-hello-ll",
29+
input1: "hello",
30+
input2: "ll",
31+
want: 2,
32+
},
33+
{
34+
name: "test-aaaaa-bba",
35+
input1: "aaaaa",
36+
input2: "bba",
37+
want: -1,
38+
},
39+
}
40+
41+
testLog := leet.NewTestLog(len(tests))
42+
defer testLog.Render()
43+
44+
timeoutDuration := time.Second * 2
45+
46+
for idx, test := range tests {
47+
// 超时检测
48+
got := test.want
49+
timeout := leet.Timeout(timeoutDuration, func(ctx context.Context, cancel context.CancelFunc) {
50+
got = solution.Export(test.input1, test.input2)
51+
cancel()
52+
})
53+
54+
if timeout {
55+
testLog.Fail(idx+1, test.name, "timeout")
56+
continue
57+
}
58+
59+
if !reflect.DeepEqual(test.want, got) {
60+
testLog.Fail(idx+1, test.name, fmt.Sprintf("want: %v, got %v.", test.want, got))
61+
continue
62+
}
63+
64+
testLog.Pass(idx+1, test.name)
65+
}
66+
}
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(haystack string, needle string) int {
10+
defer func() {
11+
if r := recover(); r != nil {
12+
fmt.Println("Params: ", haystack, needle)
13+
fmt.Println("Panic:", r)
14+
fmt.Println()
15+
debug.PrintStack()
16+
os.Exit(0)
17+
}
18+
}()
19+
20+
return strStr(haystack, needle)
21+
}
22+
23+
/****************************************************/
24+
/******** 以下为 Leetcode 示例部分(提交PR请还原) *******/
25+
/******** 使用 (./leetcode clear) 初始化所有问题 *******/
26+
/****************************************************/
27+
28+
func strStr(haystack string, needle string) int {
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(haystack string, needle string) int {
10+
defer func() {
11+
if r := recover(); r != nil {
12+
fmt.Println("Params: ", haystack, needle)
13+
fmt.Println("Panic:", r)
14+
fmt.Println()
15+
debug.PrintStack()
16+
os.Exit(0)
17+
}
18+
}()
19+
20+
return strStr(haystack, needle)
21+
}
22+
23+
/****************************************************/
24+
/******** 以下为 Leetcode 示例部分(提交PR请还原) *******/
25+
/******** 使用 (./leetcode clear) 初始化所有问题 *******/
26+
/****************************************************/
27+
28+
func strStr(haystack string, needle string) int {
29+
30+
}

0 commit comments

Comments
 (0)