-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
41 lines (39 loc) · 807 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
}