[LeetCode] Palindrome Partition [11]

题目

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“,你需要返回["aa","b"],["a","a","b"]。这个题目的解法也是非常的典型---循环加递归,这是解决组合排列问题的标准解法。Leetcode上面还有许多这种解法的题目。

对于aab:

判断a,再判断ab;

ab:先判断a,在判断b;

b:判断完,结束

判断aa,再判断b

b:判断完,结束

其余的看代码理解吧。

代码实现

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

    void helper(int start, const string &s, vector<string> part, vector<vector<string> > &ret){
        int n = s.size();
        if(start == n){
            ret.push_back(part);
            return ;
        }
        for(int i=start; i<n; ++i){
            string temp = s.substr(start, i-start+1);
            if( !validPalindrome(temp)) continue;
            part.push_back(temp);
            helper(i+1, s, part, ret);
            part.pop_back();
        }
    }

    bool validPalindrome(string s){
        int begin = 0, end = s.size()-1;
        while(begin<end){
            if(s[begin] != s[end]) return false;
            ++begin, --end;
        }
        return true;
    }
};

如果你觉得本篇对你有收获,请帮顶。

另外,我开通了微信公众号--分享技术之美,我会不定期的分享一些我学习的东西.

你可以搜索公众号:swalge 或者扫描下方二维码关注我

(转载文章请注明出处: http://blog.csdn.net/swagle/article/details/28889301
)

[LeetCode] Palindrome Partition [11],布布扣,bubuko.com

时间: 2024-08-04 22:17:23

[LeetCode] Palindrome Partition [11]的相关文章

LeetCode: Palindrome Partition

LeetCode: Palindrome Partition 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"], [&q

LeetCode——Palindrome Partition

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&q

LeetCode &quot;Palindrome Partition II&quot; - Double DP

A so juicy and interesting problem! Recommend to everyone! A final solution may not jump into your mind immediately, of course, DP is not a trivial topic. So let's go step by step - it is more interesting that how you get to destination than destinat

Leetcode | Palindrome

Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example,"A man, a plan, a canal: Panama" is a palindrome."race a car" is not a palindrome. Note:Have you

LeetCode --- 86. Partition List

题目链接:Partition List Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x. You should preserve the original relative order of the nodes in each of the two partitions. For example,

[leetcode]Palindrome Partitioning @ Python

原题地址:https://oj.leetcode.com/problems/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 [

LeetCode OJ - Partition List

题目: Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x. You should preserve the original relative order of the nodes in each of the two partitions. For example,Given 1->4->3-&

[LeetCode] [Palindrome Number 2012-01-04]

Determine whether an integer is a palindrome. Do this without extra space. if use recursive, like check the first dig and last dig, then remove them, check the rest, it will fail when digint like "1021", when remove the first and last one, the r

LeetCode——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","