【leetcode】131. Palindrome Partitioning

题目如下:

解题思路:一般来说,对于题目要求返回所有可能的结果的集合的情况,对算法时间复杂度的要求都不会太复杂。本题直接穷举遍历即可。

代码如下:

class Solution(object):
    def isPalindrom(self,s):
        return s == s[::-1]def isFull(self,vl,s):
        r = ''
        for i in vl:
            r += i
        return r == s

    def partition(self, s):
        """
        :type s: str
        :rtype: List[List[str]]
        """
        queue = []
        subs = ''
        for i, v in enumerate(s):
            subs += v
            if self.isPalindrom(subs) == True:
                queue.append(([subs],i))
        res = []
        while len(queue) > 0:
            vl,inx = queue.pop(0)
            if self.isFull(vl,s) == True:
                res.append(vl)
                continue
            subs = ''
            for i in range(inx+1,len(s)):
                subs += s[i]
                if self.isPalindrom(subs) == True:
                    tl = vl[:]
                    tl.append(subs)
                    queue.append((tl,i))

        return res

 

posted @ 2018-08-01 15:52  seyjs  阅读(149)  评论(0编辑  收藏  举报