-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path131.py
More file actions
32 lines (26 loc) · 918 Bytes
/
131.py
File metadata and controls
32 lines (26 loc) · 918 Bytes
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
from typing import List
import copy
class Solution:
def __init__(self):
self.ans: List[List[str]] = []
self.path: List[str] = []
def partition(self, s: str) -> List[List[str]]:
self.backTrack(s, 0)
return self.ans
def backTrack(self, s: str, startIndex: int):
if startIndex == len(s):
resPath: List[str] = copy.deepcopy(self.path)
self.ans.append(resPath)
currentStr = ''
for i in range(startIndex, len(s), 1):
currentStr += s[i]
if self.isPalindrome(currentStr):
self.path.append(currentStr)
#这里不叫回溯这里叫递归
self.backTrack(s, i + 1)
#别忘了回溯
self.path.pop(len(self.path) - 1)
def isPalindrome(self, s: str) -> bool:
return s == s[::-1]
so = Solution()
so.partition("aab")