Palindrome Partitioning

原创 2015年11月20日 15:37:35

1.问题描述

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

2.解答

class Solution {
public:
    vector<vector<string>> partition(string s) 
    {
        vector<string> temp;
        partition_(s,temp,0);
        return res;
    }
    void partition_(string s,vector<string> temp,int begin) 
    {
        if(begin>=s.size())
        {
            res.push_back(temp);
            return ;
        }
        vector<int> index;
        index=part(s,begin);
        for(int i=0;i<index.size();i++)
        {
            string temp1=s.substr(begin,index[i]-begin+1);
            temp.push_back(temp1);
            partition_(s,temp,index[i]+1);
            temp.pop_back();
        }
        
    }
    vector<int> part(string s,int begin)
    {
        vector<int> res;
        if(begin<s.size())
        {
            int end=begin;
            while(end<s.size())
            {
                int i=begin,j=end;
                 while(i<=j)
                 {
                    
                    if(s[i]!=s[j])
                        break;
                    i++;
                    j--;
                 }
                 if(i>j) res.push_back(end);
                 end++;
            }
        }
        return res;
    }
private:
    vector<vector<string>> res;
};


版权声明:本文为博主原创文章,未经博主允许不得转载。

[leetcode]Palindrome Partitioning II

Palindrome Partitioning II   Given a string s, partition s such that every substring of the ...

1044 - Palindrome Partitioning (记忆化搜索乱搞)

1044 - Palindrome Partitioning PDF (English) Statistics Forum ...

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

Given a string s, partition s such that every substring of the partition is a palindrome. Return ...

LeetCode OJ:Palindrome Partitioning II

Palindrome Partitioning II  Total Accepted: 3866 Total Submissions: 22882My Submissions Given a...

[LeetCode] Palindrome Partitioning II

Given a string s, partition s such that every substring of the partition is a palindrome. Return the...

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

131. Palindrome Partitioning Given a string s, partition s such that every substring of the partit...

LeetCode 之 Palindrome Partitioning II(动态规划)

LeetCode 之 Palindrome Partitioning II,本文给出基于动态规划算法思想与状态转换表达式,最后,贴上C++详细源码实现。....

[LeetCode]132. Palindrome Partitioning II

[LeetCode]132. Palindrome Partitioning II题目描述思路动态规划 开始考虑,和单词划分很像 但是简单的遍历加计算是否回文串总共需要n^3的时间复杂度 优化:...
  • Lcharon
  • Lcharon
  • 2017年04月21日 11:53
  • 126

Palindrome Partitioning II 最小的括号分割

前面一道题目是要找到所有的回文子字符组合,现在是找到最小的分割数。很显然,应该用动态规划优化。 开一个数组minValue, minValue[ i ] 存储以 index i 为起点的字符串的最小...

[Leetcode]Palindrome Partitioning II

Given a string s, partition s such that every substring of the partition is a palindrome. Return th...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Palindrome Partitioning
举报原因:
原因补充:

(最多只允许输入30个字)