28. Implement strStr() go语言

go语言

func strStr(haystack string, needle string) int {

    len_hay, len_needle := strings.Count(haystack, "") - 1, strings.Count(needle, "") - 1
 
    for i := 0;i < len_hay - len_needle + 1; i++{
        if haystack[i:i + len_needle] == needle[:]{
            return i
        } 
    }
    
    return -1
}

测试用例

"hello"
"ll"
"aaaaa"
"bba"
"ss"
""
""
"ss"
""
""
"a"
"a"

结果输出

2
-1
0
-1
0
0

猜你喜欢

转载自blog.csdn.net/dyd961121/article/details/81232605