Skip to content

Commit 5d88de6

Browse files
committed
Create 44. 分割回文串.md
1 parent 4c42134 commit 5d88de6

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

44. 分割回文串.md

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
***给你一个字符串 s,请你将 s 分割成一些子串,使每个子串都是回文串 。返回 s 所有可能的分割方案。***
2+
3+
```
4+
输入:s = "aab"
5+
输出:[["a","a","b"],["aa","b"]]
6+
```
7+
8+
```
9+
class Solution:
10+
def partition(self, s: str) -> List[List[str]]:
11+
#双指针判断是否为回文串
12+
def ispal(s):
13+
left = 0
14+
right = len(s)-1
15+
while left < right:
16+
if s[left] != s[right]:
17+
return False
18+
left += 1
19+
right -= 1
20+
return True
21+
#将要遍历的字符串索引
22+
ind = 0
23+
#当前起始索引
24+
start = 0
25+
#当前组合
26+
cur = []
27+
res = []
28+
#构造递归
29+
def dfs(ind, start, cur):
30+
#递归终止条件
31+
if ind == len(s):
32+
if ''.join(cur) == s:
33+
res.append(cur)
34+
return
35+
#情况1
36+
if ispal(s[start:ind+1]):
37+
dfs(ind+1, ind+1, cur+[s[start:ind+1]])
38+
#情况2
39+
dfs(ind+1, start, cur)
40+
#调用递归
41+
dfs(ind, start, cur)
42+
return res
43+
```

0 commit comments

Comments
 (0)