LeetCode: Palindrome Partitioning
2014-04-06 15:13
197 查看
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 =
Return
Return all possible palindrome partitioning of s.
For example, given s =
"aab",
Return
[ ["aa","b"], ["a","a","b"] ] 对于这类返回所有结果集合的问题,通常都是使用DFS,递归算法 Soluton:
class Solution { public: bool valid(string &str, int st, int end){ while (st < end){ if (str[end] != str[st]){ return false; }else{ st++; end--; } } return true; } void find(string &s, int st, vector<string> &res, vector<vector<string> > &resAll){ if (st>=s.size()){ resAll.push_back(res); }else{ for (int i=st;i<s.size();i++){ if (valid(s,st,i)){ res.push_back(s.substr(st,i-st+1)); find(s,i+1,res,resAll); res.pop_back(); } } } } vector<vector<string>> partition(string s) { // Start typing your C/C++ solution below // DO NOT write int main() function vector<vector<string> > resAll; vector<string> res; find(s,0,res,resAll); return resAll; } };
相关文章推荐
- sort
- 关于回调机制的例子
- C++中 模板Template的使用
- LinearLayout布局
- Packmol 的安装
- [转]log4j.properties log4j.xml 路径问题
- Python 之getpass模块
- 第九章 子窗口控件
- HDU 1465(错排)
- 第5周作业-贷款计算器程序
- DOM操作样式表
- Word Ladder -- LeetCode
- 深入java基础
- back_inserter
- Sql Server中不常用的表运算符之PIVOT
- Mesh Algorithm in OpenCascade
- Problem D
- java基础13
- 算法设计的一般方法概览
- KEIL3中出现的字符不对齐的情况解决办法