This commit is contained in:
ehlxr 2022-06-19 16:59:41 +08:00
parent 6befec80e9
commit 80212df249
3 changed files with 37 additions and 19 deletions

20
main.go
View File

@ -1,10 +1,6 @@
package main package main
import ( import "fmt"
"fmt"
. "github.com/ehlxr/leetcode-go/src"
)
func main() { func main() {
// node5 := &ListNode{Val: 5, Next: nil} // node5 := &ListNode{Val: 5, Next: nil}
@ -19,17 +15,11 @@ func main() {
// fmt.Printf("%v\n", ReverseRec(root)) // fmt.Printf("%v\n", ReverseRec(root))
// fmt.Printf("%v\n", ReverseKGroup(root, 2)) // fmt.Printf("%v\n", ReverseKGroup(root, 2))
// s := "Hello 你好" s := "Hello 你好"
// println(s[2], s[6]) fmt.Println(s[2], s[6])
// rs := []rune(s) rs := []rune(s)
// println(string(rs[6])) println(string(rs[6]))
// s := "babad" // s := "babad"
// println(LongestPalindrome(s)) // println(LongestPalindrome(s))
s := "bacbababaabcbab"
p := "ababa"
// fmt.Printf("%+v\n", GetNext(p))
fmt.Printf("%+v\n", Kmp(s, p))
fmt.Printf("%+v", Bf(s, p))
} }

View File

@ -1,7 +1,7 @@
package src package src
// 暴力匹配 // 暴力匹配
func Bf(s, p string) int { func bf(s, p string) int {
n := len(s) n := len(s)
m := len(p) m := len(p)
if n < m { if n < m {
@ -25,13 +25,13 @@ func Bf(s, p string) int {
return -1 return -1
} }
func Kmp(s, p string) int { func kmp(s, p string) int {
n := len(s) n := len(s)
m := len(p) m := len(p)
if n < m { if n < m {
return -1 return -1
} }
next := GetNext(p) next := getNext(p)
for i := 0; i <= n-m; i++ { for i := 0; i <= n-m; i++ {
j := 0 j := 0
@ -60,7 +60,7 @@ func Kmp(s, p string) int {
return -1 return -1
} }
func GetNext(p string) []int { func getNext(p string) []int {
n := len(p) n := len(p)
next := make([]int, n) next := make([]int, n)
next[0] = 0 next[0] = 0

28
src/string_match_test.go Normal file
View File

@ -0,0 +1,28 @@
package src
import (
"fmt"
"testing"
)
func TestBf(t *testing.T) {
s := "bacbababaabcbab"
p := "ababa"
fmt.Printf("bf %s match %s index is: %+v\n", p, s, bf(s, p))
// t.Logf("%s match %s index is: %+v\n", p, s, bf(s, p))
}
func TestGetNext(t *testing.T) {
p := "ababa"
fmt.Printf("getNext %s next is: %+v\n", p, getNext(p))
// t.Logf("%s next is: %+v\n", p, next)
}
func TestKmp(t *testing.T) {
s := "bacbababaabcbab"
p := "ababa"
fmt.Printf("kmp %s match %s index is: %+v\n", p, s, kmp(s, p))
// t.Logf("%s match %s index is: %+v\n", p, s, kmp(s, p))
}