-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path459.py
60 lines (44 loc) · 1.6 KB
/
459.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# Leetcode 459 重复的子字符串
# 第一种方法 使用KMP去做
class Solution:
def repeatedSubstringPattern(self, s: str) -> bool:
# 首先把模式串的next数组构建出来
nextlist = self.getNext(s)
# 定义在主串和模式串中遍历的双指针
i, j = 1, 0
# 这个是合成的匹配串
ss = s+s
while i < len(ss)-1:
# 模式串和主串的字符判定一致了就继续挪指针
if ss[i] == s[j]:
i += 1
j += 1
elif j > 0:
j = nextlist[j - 1]
else:
i += 1
if j == len(s):
return True
return False
def getNext(self, numlist):
# 定义一个要输出的数组 这个数组的第一位肯定是0
nextlist = [0]
nextprefix = 0 # 当前已经有多少个相同前后缀的长度
i = 1
while i < len(numlist):
if numlist[nextprefix] == numlist[i]:
nextprefix += 1
nextlist.append(nextprefix)
i += 1
else:
if nextprefix == 0:
nextlist.append(nextprefix)
i += 1
else:
nextprefix = nextlist[nextprefix - 1]
return nextlist
# 第二种方法 适用py的内置函数 find 返回匹配索引的第一个下标
# 一行代码直接秒了 确实爽到爆炸
class Solution:
def repeatedSubstringPattern(self, s: str) -> bool:
return True if (s+s).find(s, 1)!= len(s) else False