8000 feat: 28 复习 KMP · chenshiwei-io/leetcode@dee3612 · GitHub
[go: up one dir, main page]

Skip to content

Commit dee3612

Browse files
author
chen-shiwei
committed
feat: 28 复习 KMP
1 parent 73988cf commit dee3612

File tree

2 files changed

+4
-6
lines changed

2 files changed

+4
-6
lines changed

leetcode/28.实现strStr/strStr.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package _8_实现strStr
22

3-
import "fmt"
4-
53
func strStr(haystack string, needle string) int {
64
l1 := len(haystack)
75
l2 := len(needle)
@@ -25,8 +23,7 @@ func strStr(haystack string, needle string) int {
2523
func strStrWithKMP(haystack string, needle string) int {
2624
l1 := len(haystack)
2725
l2 := len(needle)
28-
29-
next := make([]int, l2)
26+
var next = make([]int, l2)
3027
for i, j := 1, 0; i < l2; i++ {
3128
for j > 0 && needle[i] != needle[j] {
3229
j = next[j-1]
@@ -36,7 +33,7 @@ func strStrWithKMP(haystack string, needle string) int {
3633
}
3734
next[i] = j
3835
}
39-
fmt.Println(next)
36+
4037
for i, j := 0, 0; i < l1; i++ {
4138
for j > 0 && haystack[i] != needle[j] {
4239
j = next[j-1]
@@ -49,4 +46,5 @@ func strStrWithKMP(haystack string, needle string) int {
4946
}
5047
}
5148
return -1
49+
5250
}

leetcode/28.实现strStr/strStr_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func Test_strStr(t *testing.T) {
3030
// }
3131
//})
3232
t.Run(tt.name, func(t *testing.T) {
33-
if got := strStrWithKMP(tt.args.haystack, tt.args.needle); got != tt.want {
33+
if got := strStrWithKMP1(tt.args.haystack, tt.args.needle); got != tt.want {
3434
t.Errorf("strStrWithKMP() = %v, want %v", got, tt.want)
3535
}
3636
})

0 commit comments

Comments
 (0)
0