[LeetCode] Palindrome Partition [11]
来源:互联网 发布:海淀行知实验学校 编辑:程序博客网 时间:2024/06/05 17:12
题目
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"] ]
原题链接
解题思路
回文划分,题目意思是给一个字符串,找出将字符串划分为一系列回文子串的各种可能组合。例如,一个子串“aab“,你需要返回["aa","b"],["a","a","b"]。这个题目的解法也是非常的典型---循环加递归,这是解决组合排列问题的标准解法。Leetcode上面还有许多这种解法的题目。
对于aab:
判断a,再判断ab;
ab:先判断a,在判断b;
b:判断完,结束
判断aa,再判断b
b:判断完,结束
其余的看代码理解吧。
代码实现
class Solution {public: vector<vector<string> > partition(string s) { vector<vector<string> > ret; vector<string> part; helper(0, s, part, ret); return ret; } void helper(int start, const string &s, vector<string> part, vector<vector<string> > &ret){ int n = s.size(); if(start == n){ ret.push_back(part); return ; } for(int i=start; i<n; ++i){ string temp = s.substr(start, i-start+1); if( !validPalindrome(temp)) continue; part.push_back(temp); helper(i+1, s, part, ret); part.pop_back(); } } bool validPalindrome(string s){ int begin = 0, end = s.size()-1; while(begin<end){ if(s[begin] != s[end]) return false; ++begin, --end; } return true; }};
如果你觉得本篇对你有收获,请帮顶。
另外,我开通了微信公众号--分享技术之美,我会不定期的分享一些我学习的东西.
另外,我开通了微信公众号--分享技术之美,我会不定期的分享一些我学习的东西.
你可以搜索公众号:swalge 或者扫描下方二维码关注我
(转载文章请注明出处: http://blog.csdn.net/swagle/article/details/28889301 )
5 0
- [LeetCode] Palindrome Partition [11]
- LeetCode 131: Palindrome Partition
- [LeetCode] Palindrome Partition
- LeetCode: Palindrome Partition
- leetcode palindrome partition II
- [LeetCode] 064: Palindrome Partition
- LeetCode——Palindrome Partition
- LeetCode —— Palindrome Partition II
- Palindrome Partition
- palindrome partition II
- palindrome-partition-ii
- LeetCode :: Palindrome
- LeetCode(Palindrome partition 2) 求将一个字符串划分成回文子串 需要分成的段数最少是多少
- Two pointers (2) -- Partition List,Valid Palindrome
- LeetCode: Partition List
- LeetCode Partition List
- LeetCode: Partition List
- [Leetcode] Partition List **
- 两种最基本的MOS管——N沟道与P沟道
- 关于ALV双击 调用事务的一个例子
- MFC中播放声音
- ibatis插入数据乱码问题
- Linux下Tomcat重新启动
- [LeetCode] Palindrome Partition [11]
- 计算机网络年会:手机安全状况面临巨大威胁
- opencv1.0 在 ubuntu 下的安装遇到的问题
- 反汇编学习之浮点数
- 什么是脏读,不可重复读,幻读
- 为生活可以忍,侮辱技术行不行?
- Dialog(八)——改变系统自带Dialog字体大小(ContextThemeWrapper)
- 我第一次把一个程序分成多个源文件, 我不知道该把什么放到.c 文件, 把什么放到.h 文件。(“.h” 到底是什么意思?)
- iOS开发 - "Cast from pointer to smaller type 'int' loses information” 解决办法