[LeetCode] Palindrome Partitioning, Solution
2016-01-12 11:10
302 查看
Given a string s[/i], partition s[/i] such that every substring of the partition is a palindrome.
Return all possible palindrome partitioning of s[/i].
For example, given s[/i] =
Return
» Solve this problem
[Thoughts]
这种需要输出所有结果的基本上都是DFS的解法。实现如下。
Return all possible palindrome partitioning of s[/i].
For example, given s[/i] =
"aab",
Return
[ ["aa","b"], ["a","a","b"] ]
» Solve this problem
[Thoughts]
这种需要输出所有结果的基本上都是DFS的解法。实现如下。
[code]1: vector<vector<string>> partition(string s) { 2: vector<vector<string>> result; 3: vector<string> output; 4: DFS(s, 0, output, result); 5: return result; 6: } 7: void DFS(string &s, int start, vector<string>& output, vector<vector<string>> &result) 8: { 9: if(start == s.size()) 10: { 11: result.push_back(output); 12: return; 13: } 14: for(int i = start; i< s.size(); i++) 15: { 16: if(isPalindrome(s, start, i)) 17: { 18: output.push_back(s.substr(start, i-start+1)); 19: DFS(s, i+1, output, result); 20: output.pop_back(); 21: } 22: } 23: } 24: bool isPalindrome(string &s, int start, int end) 25: { 26: while(start< end) 27: { 28: if(s[start] != s[end]) 29: return false; 30: start++; end--; 31: } 32: return true; 33: }
相关文章推荐
- [LeetCode] Palindrome Partitioning II, Solution
- [LeetCode] Two Sum, Solution
- [LeetCode] Longest Valid Parentheses, Solution
- [LeetCode] Merge Two Sorted Lists, Solution
- C#程序通过模板自动创建Word文档
- 使用UIImageView的画线方法
- python生成随机数组
- iOS 真机测试经常出现的不能运行的问题及解决方案
- Content Provider基础(一)初识和访问其它程序中数据的方法
- 关于 mysql中非null判断
- [LeetCode] Text Justification 解题报告
- [LeetCode] Trapping Rain Water 解题报告
- [LeetCode] Triangle 解题报告
- [LeetCode] Unique Paths 解题报告
- [LeetCode] Unique Paths II 解题报告
- [LeetCode] Valid Parentheses 解题报告
- [LeetCode] Valid Sudoku 解题报告
- [LeetCode] Validate Binary Search Tree 解题报告
- [LeetCode] Wildcard Matching, Solution
- [LeetCode] Word Search 解题报告