Re: [闲聊] 每日LeetCode

楼主: pandix (面包屌)   2023-01-22 11:20:55
※ 引述《Rushia (みけねこ的鼻屎)》之铭言:
: 131. Palindrome Partitioning
: 给你一个字串s,我们可以把字串切分,找出所有可以让字串s的子字串都是回文的切法。
: Example :
: Input: s = "aab"
: Output: [["a","a","b"],["aa","b"]]
: 思路:
1.连续好几天的 DFS,往后尝试切回文字串,塞进参数 array 里然后继续切
class Solution:
def partition(self, s: str) -> List[List[str]]:
n = len(s)
res = []
def dfs(idx, arr):
if idx == n:
res.append(list(arr))
return
for i in range(idx+1, n+1):
if s[idx:i] == s[idx:i][::-1]:
dfs(i, arr+[s[idx:i]])
return
dfs(0, [])
return res
大过年的就是要刷题
作者: Rushia (みけねこ的鼻屎)   2023-01-22 11:22:00
大师
作者: PogChampLUL (火车站肥宅)   2023-01-22 11:33:00
大师
作者: SecondRun (雨夜琴声)   2023-01-22 11:35:00
大师

Links booklink

Contact Us: admin [ a t ] ucptt.com