mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 08:50:15 +08:00
Update 0459.重复的子字符串.md
This commit is contained in:
@ -264,8 +264,7 @@ class Solution {
|
||||
|
||||
Python:
|
||||
|
||||
这里使用了前缀表统一减一的实现方式
|
||||
|
||||
(版本一) 前缀表 减一
|
||||
```python
|
||||
class Solution:
|
||||
def repeatedSubstringPattern(self, s: str) -> bool:
|
||||
@ -289,7 +288,7 @@ class Solution:
|
||||
return nxt
|
||||
```
|
||||
|
||||
前缀表(不减一)的代码实现
|
||||
(版本二) 前缀表 不减一
|
||||
|
||||
```python
|
||||
class Solution:
|
||||
@ -314,6 +313,40 @@ class Solution:
|
||||
return nxt
|
||||
```
|
||||
|
||||
|
||||
(版本三) 使用 find
|
||||
|
||||
```python
|
||||
class Solution:
|
||||
def repeatedSubstringPattern(self, s: str) -> bool:
|
||||
n = len(s)
|
||||
if n <= 1:
|
||||
return False
|
||||
ss = s[1:] + s[:-1]
|
||||
print(ss.find(s))
|
||||
return ss.find(s) != -1
|
||||
```
|
||||
|
||||
(版本四) 暴力法
|
||||
|
||||
```python
|
||||
class Solution:
|
||||
def repeatedSubstringPattern(self, s: str) -> bool:
|
||||
n = len(s)
|
||||
if n <= 1:
|
||||
return False
|
||||
|
||||
substr = ""
|
||||
for i in range(1, n//2 + 1):
|
||||
if n % i == 0:
|
||||
substr = s[:i]
|
||||
if substr * (n//i) == s:
|
||||
return True
|
||||
|
||||
return False
|
||||
```
|
||||
|
||||
|
||||
Go:
|
||||
|
||||
这里使用了前缀表统一减一的实现方式
|
||||
|
Reference in New Issue
Block a user