Re: [閒聊] 每日LeetCode
※ 引述《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
大過年的就是要刷題
--
蛤?
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 123.252.3.181 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1674357659.A.02B.html
→
01/22 11:22,
2年前
, 1F
01/22 11:22, 1F
推
01/22 11:33,
2年前
, 2F
01/22 11:33, 2F
推
01/22 11:35,
2年前
, 3F
01/22 11:35, 3F
推
01/22 14:11,
2年前
, 4F
01/22 14:11, 4F
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 204 之 719 篇):