forked from yanglei-github/Leetcode_in_python3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
131_分割回文串.py
102 lines (90 loc) · 2.92 KB
/
131_分割回文串.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 15 12:30:41 2020
@author: leiya
"""
#start_index用来表示每次找到子串的起始位置,for i in start_index表示以
#start_index开始找到结尾index i
class Solution:
def partition(self, s: str) -> List[List[str]]:
if not s:
return []
def dfs(s,start_index,path,res):
if start_index == len(s):
res.append(path[:])
return
for i in range(start_index,len(s)):
if s[start_index:i+1] == s[start_index:i+1][::-1]:
path.append(s[start_index:i+1])
dfs(s,i+1,path,res)
path.pop()
else:
continue
res = []
path = []
start_index = 0
dfs(s,start_index,path,res)
return res
#超时
class Solution:
def partition(self, s: str) -> List[List[str]]:
if not s:
return []
def dfs(s,start_index,path,res):
if start_index == len(s):
res.append(path[:])
return
for i in range(start_index,len(s)):
if not isPalindrome(s,start_index,i):
continue
else:
path.append(s[start_index:i+1])
dfs(s,i+1,path,res)
path.pop()
def isPalindrome(s,left,right):
while left < right:
if s[left] != s[right]:
False
else:
left += 1
right -= 1
return True
res = []
path = []
start_index = 0
dfs(s,start_index,path,res)
return res
#引入dp,用到的是leetcode.5最长回文串的dp思路
class Solution:
def partition(self, s: str) -> List[List[str]]:
if not s:
return []
length = len(s)
dp = [[False for _ in range(length)] for _ in range(length)]
for i in range(length):
dp[i][i] = True
for j in range(1, length):
for i in range(0, j):
if s[i] == s[j]:
if j - i < 3:
dp[i][j] = True
else:
dp[i][j] = dp[i + 1][j - 1]
else:
dp[i][j] = False
def dfs(s,start_index,path,res):
if start_index == len(s):
res.append(path[:])
return
for i in range(start_index,len(s)):
if not dp[start_index][i]:
continue
else:
path.append(s[start_index:i+1])
dfs(s,i+1,path,res)
path.pop()
res = []
path = []
start_index = 0
dfs(s,start_index,path,res)
return res