关闭

Leetcode164: Palindrome Partitioning

标签: leetcodealgorithm
122人阅读 评论(0) 收藏 举报
分类:

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"]
  ]
class Solution {
public:
    void dfs(string s, vector<string> &path, vector<vector<string>> &res)  
    {  
        if(s.size() < 1)  
        {  
            res.push_back(path);  
            return;  
        }  
        for(int i = 0; i < s.size(); i++)  
        {  
            int begin = 0;  
            int end = i;  
            while(begin < end)  
            {  
                if(s[begin] == s[end])  
                {  
                    begin++;  
                    end--;  
                }  
                else  
                    break;  
            }  
            if(begin >= end)//bool isPalindrome = true;  
            {  
                path.push_back(s.substr(0,i+1));  
                dfs(s.substr(i+1),path,res);  
                path.pop_back();  
            }  
        }  
    }  
    vector<vector<string>> partition(string s) {  
        // IMPORTANT: Please reset any member data you declared, as  
        // the same Solution instance will be reused for each test case.  
        vector<vector<string>> res;  
        vector<string> path;  
        dfs(s,path,res);  
        return res;  
    }  
};



0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:70912次
    • 积分:2369
    • 等级:
    • 排名:第17240名
    • 原创:148篇
    • 转载:119篇
    • 译文:0篇
    • 评论:6条
    个人主页
    文章分类
    最新评论