28.go 294 B

1234567891011121314151617
  1. package main
  2. // search needle in haystack
  3. func strStr(haystack string, needle string) int {
  4. for i := 0; i <= len(haystack)-len(needle); i++ {
  5. j := 0
  6. for ; j < len(needle); j++ {
  7. if haystack[i+j] != needle[j] {
  8. break
  9. }
  10. }
  11. if j == len(needle) {
  12. return i
  13. }
  14. }
  15. return -1
  16. }