leetcode Palindrome Partitioning
来源:互联网 发布:mac版的优酷能缓存吗 编辑:程序博客网 时间:2024/06/06 07:07
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"] ]
详细说明:
str1 = str.substr(0, i); 取出前面下标从 0 开始到 i 结束的子串,判断str1是否满足回文字符串的要求,
1. 满足:这样子,有可能成为一种分割的情况,所以我们new出一个list集合,把str1放入到list中,然后我们求出str剩余的子串 str2 = str.substr(i); 取出前面下标从 i 开始到整个字符串结尾的子串, 然后将str2 作为新的字符串,同时把list集合也传入到函数中,递归调用。递归的结束条件就是到传入的这个字符串的长度为0(这样就意味着已经到了字符串的末尾了),此时证明这种情况下得到的list集合是满足条件的,我们把这个list集合 加入到 结果集合result中。
2. 不满足的话,继续 i++, 直到 i == str.length();
3. 全部结束之后,返回 最终的结果集合 result
class Solution {
/*思想:将s一点点分解,在将剩下的考虑 DFS*/public: vector<vector<string>> partition(string s) { vector<vector<string>> result; vector<string> aresult; sub(s,result,aresult); return result; } bool ispalindrome(string s){ bool result=true; int start=0; int end=s.length()-1; while(start<end){ if(s[start++]!=s[end--]){ result=false; return result; } } return result; } void sub(string &s,vector<vector<string>> &re,vector<string> &aresult){ if(s.length()==0) re.push_back(aresult); int len=s.length(); for(int i=1;i<=len;i++){ string pa=s.substr(0,i); if(ispalindrome(pa)){ aresult.push_back(pa); string next=s.substr(i); sub(next,re,aresult); aresult.pop_back();; } } }};
0 0
- 【leetcode】Palindrome Partitioning && Palindrome Partitioning II
- [LeetCode] Palindrome Partitioning && Palindrome Partitioning II
- LeetCode Palindrome Partitioning I&&Palindrome Partitioning II
- LeetCode Palindrome Partitioning, Palindrome Partitioning II
- [Leetcode][python]Palindrome Partitioning/Palindrome Partitioning II
- Leetcode:Palindrome Partitioning & Palindrome Partitioning II
- LeetCode: Palindrome Partitioning
- LeetCode : Palindrome Partitioning II
- [leetcode] Palindrome Partitioning II
- leetcode 122: Palindrome Partitioning
- leetcode:Palindrome Partitioning II
- leetcode:Palindrome Partitioning
- 【leetcode】Palindrome Partitioning
- 【leetcode】Palindrome Partitioning II
- leetcode - Palindrome Partitioning II
- Leetcode - Palindrome Partitioning
- LeetCode之Palindrome Partitioning
- [LeetCode]Palindrome Partitioning
- 编写ADF 验证
- 用ListView搭建手机通讯录
- GROUP BY的用法
- 开发中的自动化——按键精灵&AutoIt
- Linux 服务器监控命令
- leetcode Palindrome Partitioning
- STM32时钟设置
- 《数据结构和算法分析》学习之散列表
- HTML5学习笔记之二CSS基础
- typedef int (init_fnc_t) (void)和typedef int (*init_fnc_t) (void)
- ubuntu下如何安装flash
- 想法于创新
- Objective C Foundation框架(NSString)
- UIApplication代理简介