mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
Merge pull request #1234 from Amberling1988/master
Update 0459.重复的子字符串.md 添加swift方法(前缀表统一不减一)
This commit is contained in:
@ -1059,5 +1059,112 @@ func getNext(_ next: inout [Int], needle: [Character]) {
|
||||
|
||||
```
|
||||
|
||||
> 前缀表右移
|
||||
|
||||
```swift
|
||||
func strStr(_ haystack: String, _ needle: String) -> Int {
|
||||
|
||||
let s = Array(haystack), p = Array(needle)
|
||||
guard p.count != 0 else { return 0 }
|
||||
|
||||
var j = 0
|
||||
var next = [Int].init(repeating: 0, count: p.count)
|
||||
getNext(&next, p)
|
||||
|
||||
for i in 0 ..< s.count {
|
||||
|
||||
while j > 0 && s[i] != p[j] {
|
||||
j = next[j]
|
||||
}
|
||||
|
||||
if s[i] == p[j] {
|
||||
j += 1
|
||||
}
|
||||
|
||||
if j == p.count {
|
||||
return i - p.count + 1
|
||||
}
|
||||
}
|
||||
|
||||
return -1
|
||||
}
|
||||
|
||||
// 前缀表后移一位,首位用 -1 填充
|
||||
func getNext(_ next: inout [Int], _ needle: [Character]) {
|
||||
|
||||
guard needle.count > 1 else { return }
|
||||
|
||||
var j = 0
|
||||
next[0] = j
|
||||
|
||||
for i in 1 ..< needle.count-1 {
|
||||
|
||||
while j > 0 && needle[i] != needle[j] {
|
||||
j = next[j-1]
|
||||
}
|
||||
|
||||
if needle[i] == needle[j] {
|
||||
j += 1
|
||||
}
|
||||
|
||||
next[i] = j
|
||||
}
|
||||
next.removeLast()
|
||||
next.insert(-1, at: 0)
|
||||
}
|
||||
```
|
||||
|
||||
> 前缀表统一不减一
|
||||
```swift
|
||||
|
||||
func strStr(_ haystack: String, _ needle: String) -> Int {
|
||||
|
||||
let s = Array(haystack), p = Array(needle)
|
||||
guard p.count != 0 else { return 0 }
|
||||
|
||||
var j = 0
|
||||
var next = [Int](repeating: 0, count: needle.count)
|
||||
// KMP
|
||||
getNext(&next, needle: p)
|
||||
|
||||
for i in 0 ..< s.count {
|
||||
while j > 0 && s[i] != p[j] {
|
||||
j = next[j-1]
|
||||
}
|
||||
|
||||
if s[i] == p[j] {
|
||||
j += 1
|
||||
}
|
||||
|
||||
if j == p.count {
|
||||
return i - p.count + 1
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
//前缀表
|
||||
func getNext(_ next: inout [Int], needle: [Character]) {
|
||||
|
||||
var j = 0
|
||||
next[0] = j
|
||||
|
||||
for i in 1 ..< needle.count {
|
||||
|
||||
while j>0 && needle[i] != needle[j] {
|
||||
j = next[j-1]
|
||||
}
|
||||
|
||||
if needle[i] == needle[j] {
|
||||
j += 1
|
||||
}
|
||||
|
||||
next[i] = j
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
-----------------------
|
||||
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
||||
|
@ -342,7 +342,7 @@ class Solution:
|
||||
return path
|
||||
```
|
||||
|
||||
### Go
|
||||
### GO
|
||||
```go
|
||||
type pair struct {
|
||||
target string
|
||||
|
@ -421,5 +421,89 @@ 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
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
> 前缀表统一不减一
|
||||
```swift
|
||||
func repeatedSubstringPattern(_ s: String) -> Bool {
|
||||
|
||||
let sArr = Array(s)
|
||||
let len = sArr.count
|
||||
if len == 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
var next = Array.init(repeating: 0, count: len)
|
||||
getNext(&next, sArr)
|
||||
|
||||
if next[len-1] != 0 && len % (len - next[len-1]) == 0 {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// 前缀表不减一
|
||||
func getNext(_ next: inout [Int], _ sArr:[Character]) {
|
||||
|
||||
var j = 0
|
||||
next[0] = 0
|
||||
|
||||
for i in 1 ..< sArr.count {
|
||||
|
||||
while j > 0 && sArr[i] != sArr[j] {
|
||||
j = next[j-1]
|
||||
}
|
||||
|
||||
if sArr[i] == sArr[j] {
|
||||
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