美文网首页
131. Palindrome Partitioning

131. Palindrome Partitioning

作者: April63 | 来源:发表于2018-06-28 11:05 被阅读0次

很典型的一个题目,字符串分割求子集的一个题目,用一个指针指示位置,递归法

class Solution(object):
    def partition(self, s):
        """
        :type s: str
        :rtype: List[List[str]]
        """
        res = []
        self.surpport(res, [], s, 0)
        return res
    def surpport(self, res, temp, s, start):
        if start == len(s):
            res.append(temp)
            return 
        for i in range(start, len(s)):
            if self.palindrome(s, start, i):
                self.surpport(res, temp +[s[start:i+1]], s, i+1)

    def palindrome(self, s, low, high ):
        while low <= high:
            if s[low] == s[high]:
                low += 1
                high -= 1
            else:
                return False
        return True

相关文章

网友评论

      本文标题:131. Palindrome Partitioning

      本文链接:https://www.haomeiwen.com/subject/hmcgyftx.html