題目
題目大意
解題思路
Big O
來源
解答
https://github.com/kimi0230/LeetcodeGolang/blob/master/Leetcode/0028.Find-the-Index-of-the-First-Occurrence-in-a-String/main.go
package findtheindexofthefirstoccurrenceinastring
// 暴力解
// 時間複雜 O(M*N), 空間複雜 O()
func strStr(haystack string, needle string) int {
haystackLen := len(haystack)
needleLen := len(needle)
index := 0
for i := 0; i <= (haystackLen - needleLen); i++ {
j := 0
for j = 0; j < needleLen; j++ {
if haystack[i+j] == needle[j] {
index = i
} else {
break
}
}
if j == needleLen {
return index
}
}
return -1
}
// Slice 解法
func strStrSlice(haystack string, needle string) int {
haystackLen := len(haystack)
needleLen := len(needle)
if haystackLen == 0 || haystackLen < needleLen {
return -1
}
if needleLen == 0 {
return 0
}
for i := 0; i <= (haystackLen - needleLen); i++ {
if haystack[i:i+needleLen] == needle {
return i
}
}
return -1
}
Benchmark