10.go 371 B

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