LX

潜下心好好学习

Leetcode164: 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"]
  ]
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;  
    }  
};



阅读更多
文章标签: leetcode algorithm
个人分类: leetcode c++
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

Leetcode164: Palindrome Partitioning

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭