Skip to content

Commit 8511bd4

Browse files
Merge pull request #317 from liumingzhuo/master
0459.重复的子字符串和0028.strStr Go语言
2 parents 81a11df + 7bf42a8 commit 8511bd4

File tree

2 files changed

+148
-2
lines changed

2 files changed

+148
-2
lines changed

problems/0028.实现strStr.md

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -726,10 +726,98 @@ class Solution:
726726

727727
Go:
728728

729+
```go
730+
// 方法一:前缀表使用减1实现
731+
732+
// getNext 构造前缀表next
733+
// params:
734+
// next 前缀表数组
735+
// s 模式串
736+
func getNext(next []int, s string) {
737+
j := -1 // j表示 最长相等前后缀长度
738+
next[0] = j
739+
740+
for i := 1; i < len(s); i++ {
741+
for j >= 0 && s[i] != s[j+1] {
742+
j = next[j] // 回退前一位
743+
}
744+
if s[i] == s[j+1] {
745+
j++
746+
}
747+
next[i] = j // next[i]是i(包括i)之前的最长相等前后缀长度
748+
}
749+
}
750+
func strStr(haystack string, needle string) int {
751+
if len(needle) == 0 {
752+
return 0
753+
}
754+
next := make([]int, len(needle))
755+
getNext(next, needle)
756+
j := -1 // 模式串的起始位置 next为-1 因此也为-1
757+
for i := 0; i < len(haystack); i++ {
758+
for j >= 0 && haystack[i] != needle[j+1] {
759+
j = next[j] // 寻找下一个匹配点
760+
}
761+
if haystack[i] == needle[j+1] {
762+
j++
763+
}
764+
if j == len(needle)-1 { // j指向了模式串的末尾
765+
return i - len(needle) + 1
766+
}
767+
}
768+
return -1
769+
}
770+
```
771+
772+
```go
773+
// 方法二: 前缀表无减一或者右移
774+
775+
// getNext 构造前缀表next
776+
// params:
777+
// next 前缀表数组
778+
// s 模式串
779+
func getNext(next []int, s string) {
780+
j := 0
781+
next[0] = j
782+
for i := 1; i < len(s); i++ {
783+
for j > 0 && s[i] != s[j] {
784+
j = next[j-1]
785+
}
786+
if s[i] == s[j] {
787+
j++
788+
}
789+
next[i] = j
790+
}
791+
}
792+
func strStr(haystack string, needle string) int {
793+
n := len(needle)
794+
if n == 0 {
795+
return 0
796+
}
797+
j := 0
798+
next := make([]int, n)
799+
getNext(next, needle)
800+
for i := 0; i < len(haystack); i++ {
801+
for j > 0 && haystack[i] != needle[j] {
802+
j = next[j-1] // 回退到j的前一位
803+
}
804+
if haystack[i] == needle[j] {
805+
j++
806+
}
807+
if j == n {
808+
return i - n + 1
809+
}
810+
}
811+
return -1
812+
}
813+
```
814+
815+
816+
729817

730818

731819
-----------------------
732820
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
733821
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
734822
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
735-
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
823+
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>

problems/0459.重复的子字符串.md

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,11 +236,69 @@ class Solution:
236236

237237
Go:
238238

239+
这里使用了前缀表统一减一的实现方式
240+
241+
```go
242+
func repeatedSubstringPattern(s string) bool {
243+
n := len(s)
244+
if n == 0 {
245+
return false
246+
}
247+
next := make([]int, n)
248+
j := -1
249+
next[0] = j
250+
for i := 1; i < n; i++ {
251+
for j >= 0 && s[i] != s[j+1] {
252+
j = next[j]
253+
}
254+
if s[i] == s[j+1] {
255+
j++
256+
}
257+
next[i] = j
258+
}
259+
// next[n-1]+1 最长相同前后缀的长度
260+
if next[n-1] != -1 && n%(n-(next[n-1]+1)) == 0 {
261+
return true
262+
}
263+
return false
264+
}
265+
```
266+
267+
前缀表(不减一)的代码实现
268+
269+
```go
270+
func repeatedSubstringPattern(s string) bool {
271+
n := len(s)
272+
if n == 0 {
273+
return false
274+
}
275+
j := 0
276+
next := make([]int, n)
277+
next[0] = j
278+
for i := 1; i < n; i++ {
279+
for j > 0 && s[i] != s[j] {
280+
j = next[j-1]
281+
}
282+
if s[i] == s[j] {
283+
j++
284+
}
285+
next[i] = j
286+
}
287+
// next[n-1] 最长相同前后缀的长度
288+
if next[n-1] != 0 && n%(n-next[n-1]) == 0 {
289+
return true
290+
}
291+
return false
292+
}
293+
```
294+
295+
296+
239297

240298

241299

242300
-----------------------
243301
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
244302
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
245303
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
246-
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
304+
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>

0 commit comments

Comments
 (0)