[LeetCode]131. Palindrome Partitioning
131. Palindrome Partitioning
回溯
class Solution(object):
def partition(self, s):
"""
:type s: str
:rtype: List[List[str]]
"""
def backtracking(cur, path):
if not cur:
res.append(path[:])
return
for i in range(len(cur)):
temp = cur[:i+1]
if temp == temp[::-1]:
backtracking(cur[i+1:], path + [temp])
res = []
backtracking(s, [])
return res
class Solution(object):
def partition(self, s):
"""
:type s: str
:rtype: List[List[str]]
"""
def backtracking(cur, path):
if not cur:
return True
for i in range(len(cur)):
temp = cur[:i+1]
if temp == temp[::-1] and backtracking(cur[i+1:], path + [temp]):
res.append(path+[temp])
return False
res = []
backtracking(s, [])
return res
class Solution(object):
def partition(self, s):
"""
:type s: str
:rtype: List[List[str]]
"""
def backtracking(idx, path):
if idx == len(s):
res.append(path[:])
return
for i in range(idx, len(s)):
temp = s[idx:i+1]
if temp == temp[::-1]:
backtracking(i + 1, path + [temp])
res = []
backtracking(0, [])
return res
关注公众号:数据结构与算法那些事儿,每天一篇数据结构与算法

浙公网安备 33010602011771号