关闭

Leetcode164: Palindrome Partitioning

标签: leetcodealgorithm
133人阅读 评论(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网站的观点或立场

动态规划——回文最小分割数(palindrome-partitioning-ii)

题目: 给定一个字符串str,返回把str全部切成回文子串的最小分割数。 举例: str="ABA" ,不需要切割,返回0; str="ACDCDCDAD",最少需要切两次,比如"A"...
  • jingsuwen1
  • jingsuwen1
  • 2016-07-17 21:06
  • 6668

判断字符串string所有分割和子串是否为回文(palindrome)并返回结果(Palindrome Partitioning)

在网站http://leetcode.com/onlinejudge上 原题1是这样的:(解答见函数partition) Palindrome Partitioning Given a st...
  • loveaborn
  • loveaborn
  • 2013-03-17 20:26
  • 1564

【LeetCode】- Valid Palindrome(正确的回文)

[ 问题: ] Given a string, determine if it is a palindrome, considering only alphanumeric characters a...
  • zdp072
  • zdp072
  • 2014-08-28 08:46
  • 3179

[LeetCode] Palindrome Partitioning

Total Accepted: 9269 Total Submissions: 36157 Given a string s, partition s such that every s...
  • u014425050
  • u014425050
  • 2014-04-23 11:04
  • 426

Leetcode237: Palindrome Partitioning II

Given a string s, partition s such that every substring of the partition is a palindrome. Return ...
  • u013089961
  • u013089961
  • 2016-01-15 09:39
  • 216

[leetcode]Palindrome Partitioning II

Palindrome Partitioning II   Given a string s, partition s such that every substring of the ...
  • menglinaoxiang
  • menglinaoxiang
  • 2014-05-23 20:24
  • 366

LeetCode | Palindrome Partitioning II(最少切割次数将一个子串划分成回文子串)

Given a string s, partition s such that every substring of the partition is a palindrome. Return ...
  • a45872055555
  • a45872055555
  • 2014-08-16 10:34
  • 476

LeetCode OJ:Palindrome Partitioning II

Palindrome Partitioning II  Total Accepted: 3866 Total Submissions: 22882My Submissions Given a...
  • starcuan
  • starcuan
  • 2014-01-19 15:52
  • 674

[LeetCode] Palindrome Partitioning II

Given a string s, partition s such that every substring of the partition is a palindrome. Return the...
  • jiyanfeng1
  • jiyanfeng1
  • 2014-12-18 21:38
  • 408

LeetCode 131. Palindrome Partitioning 树形问题,状态回溯

131. Palindrome Partitioning Given a string s, partition s such that every substring of the partit...
  • wys2011101169
  • wys2011101169
  • 2017-06-05 21:03
  • 234
    个人资料
    • 访问:79067次
    • 积分:2447
    • 等级:
    • 排名:第17401名
    • 原创:148篇
    • 转载:119篇
    • 译文:0篇
    • 评论:6条
    个人主页
    文章分类
    最新评论