Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit eb0435d

Browse files
Add Solution.go for 0028.Implement strStr()
1 parent 7acd624 commit eb0435d

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
func strStr(haystack string, needle string) int {
2+
switch {
3+
case len(needle) == 0:
4+
return 0
5+
case len(needle) > len(haystack):
6+
return -1
7+
case len(needle) == len(haystack):
8+
if needle == haystack {
9+
return 0
10+
}
11+
return -1
12+
}
13+
cursor := 0
14+
for i := 0; i < len(haystack); i++ {
15+
if haystack[i] == needle[cursor] {
16+
cursor++
17+
if cursor == len(needle) {
18+
return i - cursor + 1
19+
}
20+
} else {
21+
i -= cursor
22+
cursor = 0
23+
}
24+
}
25+
return -1
26+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /