LeetCode131:Palindrome Partitioning

Given a string s, partition s such that every substring of the partition is a palindrome.

Return all possible palindrome partitioning of s.

For example, given s = “aab”,

Return

[

[“aa”,”b”],

[“a”,”a”,”b”]

]

最開始看到这道题时毫无思路。可能是看到回文就怕了。也想不出怎样用回溯来求解。

于是在纸上随便乱画了一些,结果发现好像能够依照这个思路求解了,简直囧啊。

对于上面的”aab”作为输入。能够这么寻找回文:

“a”+”ab”构成的回文串

“aa”+”b”构成的回文串

“aab”不是回文。所以直接退出。

于是感觉对于一个字符串,能够对这个字符串进行遍历,假设前pos个字符串本身是个回文字符。那么仅仅须要求解后面的子字符的回文串就可以,于是这个问题被分解成了一个更小的问题。

这道题更像一个分治法的题,将问题规模不断缩小,当然的遍历字符串的过程中须要进行回溯。

除了须要一个进行递归的辅助函数外。还须要定义一个推断一个字符串是否是回文字符串的辅助函数。程序的逻辑很easy。

这道题和Combination Sum 比較相似,一開始看到这道题时全然感觉无从下手,可是在纸上写几个測试用例,从特殊的測试用例中就能够发现规律了。加上回溯后的递归都没有那么一目了然,可能有測试用例会更easy懂一些。

runtime:20ms

class Solution {
public:
    vector<vector<string>> partition(string s) {
        vector<string> path;
        vector<vector<string>> result;
        helper(s,0,path,result);
        return result;
    }

    void helper(string s,int pos,vector<string> & path,vector<vector<string>> & result)
    {
        if(pos==s.size())
        {
            result.push_back(path);
            return ;
        }
        for(int i=pos;i<s.size();i++)
        {
            if(isPalindrome(s.substr(pos,i-pos+1)))
            {
                path.push_back(s.substr(pos,i-pos+1));
                helper(s,i+1,path,result);
                path.pop_back();
            }
        }
    }

    bool isPalindrome(string s)
    {
        int first=0;
        int end=s.size()-1;
        while(first<end)
        {
            if(s[first++]!=s[end--])
                return false;
        }
        return true;
    }
};
时间: 2024-10-09 07:50:13

LeetCode131:Palindrome Partitioning的相关文章

LeetCode131——Palindrome Partitioning

Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s. For example, given s = "aab", Return [ ["aa","b"], ["a","a","

LeetCode 131. 分割回文串(Palindrome Partitioning)

131. 分割回文串 131. Palindrome Partitioning 题目描述 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. LeetCode131. Palindrome Partitioning中等 示例: 输入: "aab" 输出: [ ??["aa","b"], ??["a","a","b"]] Java 实现 略

[LeetCode]132.Palindrome Partitioning II

题目 Given a string s, partition s such that every substring of the partition is a palindrome. Return the minimum cuts needed for a palindrome partitioning of s. For example, given s = "aab", Return 1 since the palindrome partitioning ["aa&qu

leetcode -- Palindrome Partitioning II

指责别人,看清自己 [问题描述] Given a string s, partition s such that every substring of the partition is a palindrome. Return the minimum cuts needed for a palindrome partitioning of s. For example, given s = "aab",Return 1 since the palindrome partitioning

132. Palindrome Partitioning II (String; DP)

Given a string s, partition s such that every substring of the partition is a palindrome. Return the minimum cuts needed for a palindrome partitioning of s. For example, given s = "aab",Return 1 since the palindrome partitioning ["aa",

[LeetCode][Java] Palindrome Partitioning

题目: Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s. For example, given s = "aab", Return [ ["aa","b"], ["a","a",&

Palindrome Partitioning -- leetcode

Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s. For example, given s = "aab", Return [ ["aa","b"], ["a","a","

LeetCode: Palindrome Partitioning II 解题报告

Palindrome Partitioning II Given a string s, partition s such that every substring of the partition is a palindrome. Return the minimum cuts needed for a palindrome partitioning of s. For example, given s = "aab",Return 1 since the palindrome pa

Palindrome Partitioning II

Given a string s, partition s such that every substring of the partition is a palindrome. Return the minimum cuts needed for a palindrome partitioning of s. For example, given s = "aab",Return 1 since the palindrome partitioning ["aa",