leetcode ||131、Palindrome Partitioning

problem:

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"]
  ]

Hide Tags
  Backtracking
题意:检查字符串的所有子字符串是否为回文字符串

thinking:

(1)这道题理论上不难,采用回溯法遍历所有的子字符串,再检查子字符串是否为回文字符串即可

(2)但难点在于,输出格式得满足判定程序。这一点我的输出格式没能通过。如:

           Input:"ab"Output:[["a"],["b"]]Expected:[["a","b"]]

 有 时间再研究怎么改吧

code:

class Solution {
private:
   vector > ret;
   vector tmp;
public:
    vector> partition(string s) {
        ret.clear();
        tmp.clear();                   
        if(s.size()==0)
           return ret;
        check(s,0,0);
        return ret;                   
        
    }
protected:
   void check(string s,int start, int end)
    {
        if(start>s.size()-1)
            return;
        string str=s.substr(start,end-start+1);
        if(ispalindrome(str))
           tmp.push_back(str);                                 
        if(end


你可能感兴趣的:(LeetCode)