Skip to content

Commit

Permalink
Merge pull request #76 from kylesliu/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
6boris committed May 5, 2019
2 parents 654bfec + 05ab922 commit bfe1fdf
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion src/0028.Implement-strStr-/README.md
Expand Up @@ -57,8 +57,49 @@ func strStr(haystack string, needle string) int {
return -1
}
```
### 思路2

### 思路 2
KMP 算法
```go
func strStr(haystack string, needle string) int {
prefixTable := calcPrefixTable(needle)

i, j := 0, 0
for i < len(haystack) && j < len(needle) {
if -1 == j || haystack[i] == needle[j] {
i++
j++
} else {
if j == 0 {
i++
} else {
j = prefixTable[j]
}
}
}
if j == len(needle) {
return i - j
}
return -1
}

func calcPrefixTable(str string) []int {
next := make([]int, len(str)+1)
length := 0

for i := 2; i <= len(str); i++ {
for length > 0 && str[length] != str[i-1] {
length = next[length]
}
if str[length] == str[i-1] {
length++
}
next[i] = length;
}

return next
}
```

## 结语

Expand Down

0 comments on commit bfe1fdf

Please sign in to comment.