diff --git a/main.go b/main.go index c285317..b2c092c 100644 --- a/main.go +++ b/main.go @@ -1,10 +1,6 @@ package main -import ( - "fmt" - - . "github.com/ehlxr/leetcode-go/src" -) +import "fmt" func main() { // node5 := &ListNode{Val: 5, Next: nil} @@ -19,17 +15,11 @@ func main() { // fmt.Printf("%v\n", ReverseRec(root)) // fmt.Printf("%v\n", ReverseKGroup(root, 2)) - // s := "Hello 你好" - // println(s[2], s[6]) - // rs := []rune(s) - // println(string(rs[6])) + s := "Hello 你好" + fmt.Println(s[2], s[6]) + rs := []rune(s) + println(string(rs[6])) // s := "babad" // 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)) } diff --git a/src/string_match.go b/src/string_match.go index ba9776d..f1d5dca 100644 --- a/src/string_match.go +++ b/src/string_match.go @@ -1,7 +1,7 @@ package src // 暴力匹配 -func Bf(s, p string) int { +func bf(s, p string) int { n := len(s) m := len(p) if n < m { @@ -25,13 +25,13 @@ func Bf(s, p string) int { return -1 } -func Kmp(s, p string) int { +func kmp(s, p string) int { n := len(s) m := len(p) if n < m { return -1 } - next := GetNext(p) + next := getNext(p) for i := 0; i <= n-m; i++ { j := 0 @@ -60,7 +60,7 @@ func Kmp(s, p string) int { return -1 } -func GetNext(p string) []int { +func getNext(p string) []int { n := len(p) next := make([]int, n) next[0] = 0 diff --git a/src/string_match_test.go b/src/string_match_test.go new file mode 100644 index 0000000..296ab11 --- /dev/null +++ b/src/string_match_test.go @@ -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)) +}