mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-10 12:15:58 +08:00
Update 0459.重复的子字符串.md
This commit is contained in:
@ -421,5 +421,48 @@ function repeatedSubstringPattern(s: string): boolean {
|
||||
};
|
||||
```
|
||||
|
||||
|
||||
Swift:
|
||||
|
||||
> 前缀表统一减一
|
||||
```swift
|
||||
func repeatedSubstringPattern(_ s: String) -> Bool {
|
||||
|
||||
let sArr = Array(s)
|
||||
let len = s.count
|
||||
if len == 0 {
|
||||
return false
|
||||
}
|
||||
var next = Array.init(repeating: -1, count: len)
|
||||
|
||||
getNext(&next,sArr)
|
||||
|
||||
if next.last != -1 && len % (len - (next[len-1] + 1)) == 0{
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func getNext(_ next: inout [Int], _ str:[Character]) {
|
||||
|
||||
var j = -1
|
||||
next[0] = j
|
||||
|
||||
for i in 1 ..< str.count {
|
||||
|
||||
while j >= 0 && str[j+1] != str[i] {
|
||||
j = next[j]
|
||||
}
|
||||
|
||||
if str[i] == str[j+1] {
|
||||
j += 1
|
||||
}
|
||||
|
||||
next[i] = j
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
-----------------------
|
||||
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
||||
|
Reference in New Issue
Block a user