zoukankan      html  css  js  c++  java
  • Leetcode练习(Python):回溯算法类:第131题:分割回文串:给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串。 返回 s 所有可能的分割方案。

    题目:
    分割回文串:给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串。  返回 s 所有可能的分割方案。
    思路:
    使用回溯算法的模板。
    程序:
    class Solution:
        def partition(self, s: str) -> List[List[str]]:
            if not s:
                return []
            auxiliary = []
            result = []
            def backtrack(s, auxiliary, result):
                if not s:
                    result.append(auxiliary[:])
                    return
                for index in range(1, len(s) + 1):
                    if s[: index] == s[index - 1 : : -1]:
                        auxiliary.append(s[: index])
                        backtrack(s[index :], auxiliary, result)
                        auxiliary.pop()
            backtrack(s, auxiliary, result)
            return result
  • 相关阅读:
    「CodeForces
    「POJ
    「CodeForces
    「CodeForces
    【CodeForces 717C】Potions Homework
    【CodeForces 730H】Car Repair Shop
    【CodeForces 730H】Delete Them
    【Gym 100947I】What a Mess
    j
    PDE工具箱的简单使用
  • 原文地址:https://www.cnblogs.com/zhuozige/p/12871095.html
Copyright © 2011-2022 走看看