Palindrome Partitioning
2013-07-25 14:10
127 查看
题目
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"] ]
思路
类似于 排列组合思想。其中是否是有效的回文,可以参考博文 valid palindrome
class Solution { public: vector<vector<string>> partition(string s) { // Start typing your C/C++ solution below // DO NOT write int main() function int len = s.length(); vector<vector<string> > result; if(len<1) return result; vector<string> vec; mypart(result, vec, 0, s); return result; } void mypart(vector<vector<string>> &result, vector<string> &vec, int cur, string &S) { if(cur==S.length()) { result.push_back(vec); return ; } for(int i=cur;i<S.length();i++) { int len = i-cur+1; string str = S.substr(cur,len); if(ispalind(str)) { vec.push_back(str); mypart(result, vec, i+1, S); vec.pop_back(); } } } bool ispalind(string &S) { if(S.empty()) return true; int i=0, j=S.length()-1; while(i<j) { if(S[i]!=S[j]) return false; i++; j--; } return true; } };
相关文章推荐
- 动易2006序列号破解算法公布
- C#数据结构与算法揭秘二
- JavaScript 组件之旅(二)编码实现和算法
- oracle 使用递归的性能提示测试对比
- 全排列算法的非递归实现与递归实现的方法(C++)
- php递归列出所有文件和目录的代码
- 循环 vs 递归浅谈
- 深入Javascript函数、递归与闭包(执行环境、变量对象与作用域链)使用详解
- C语言函数的递归和调用实例分析
- java 递归深入理解
- 将15位身份证补全为18位身份证的算法示例详解
- C++算法系列之日历生成的算法代码
- 1 2 3 4 5 6 7 8 9 = 110的java实现
- Sedgewick之巨著《算法》,与高德纳TAOCP一脉相承
- 【代码】Pythonの代码片段
- STL中算法
- 数据结构&算法学习
- 算法的时间复杂度
- 算法导论:选择排序的原理与实现
- PHP实现四种常用的排序算法