[LeetCode] Palindrome Partitioning 回文分割
2014-12-03 09:11
399 查看
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
output表示从0位到第start-1位之间的字符串所形成的palindrome substrings。当start=s.length()时,搜索结束并返回。
For example, given s = "aab", Return
[ ["aa","b"], ["a","a","b"] ]
output表示从0位到第start-1位之间的字符串所形成的palindrome substrings。当start=s.length()时,搜索结束并返回。
vector<vector<string>> partition(string s) { // the main function vector<vector<string>> result; vector<string> output; DFS(s, 0, output, result); return result; } void DFS(string &s, int start, vector<string>& output, vector<vector<string>> &result) {// helper function // output stores the palindrome found between 0 and start-1 if(start == s.size()) { result.push_back(output); return; } for(int i = start; i< s.size(); i++) { if(isPalindrome(s, start, i)) { output.push_back(s.substr(start, i-start+1)); DFS(s, i+1, output, result); output.pop_back(); } } } bool isPalindrome(string &s, int start, int end) {// helper function while(start< end) { if(s[start] != s[end]) return false; start++; end--; } return true; }
相关文章推荐
- LeetCode-Palindrome Partitioning-回文分割
- [LeetCode 131] - 回文分割(Palindrome Partitioning)
- [LeetCode]-Palindrome Partitioning 求回文分割的所有情况
- [LeetCode 132] - 回文分割II(Palindrome Partitioning II)
- Leetcode 131 Palindrome Partitioning 回文分割
- 分割成回文需要的最小分割数 Palindrome Partitioning II @LeetCode
- [LeetCode] 131. Palindrome Partitioning 回文分割
- LeetCode(Palindrome Partitioning) 将字符串分割成回文字符串
- Palindrome Partitioning 分割字符串为回文@LeetCode
- [LeetCode]—Palindrome Partitioning II 回文分割,求最小分割数
- LeetCode-Palindrome Partitioning II-回文分割-DP优化
- LeetCode-409-Longest Palindrome(求字符串可组成的最长回文长度)
- [编程题] LeetCode上的Palindrome(回文)类型的题目
- 【LeetCode-面试算法经典-Java实现】【05-Longest Palindromic Substring(最大回文字符串)】
- 回文分割
- [LeetCode] Partition List 分割单链表
- Leetcode 336 回文对
- [LeetCode] Valid Palindrome II 验证回文字符串之二
- leetcode第五题—最长回文字符串
- 2014再见 2015你好 【附带】[LeetCode]最长回文子串