LeetCode之Palindrome Partitioning
来源:互联网 发布:黑暗之魂 知乎 编辑:程序博客网 时间:2024/04/29 00:37
Palindrome PartitioningFeb 282099 / 7369
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"] ]
Run Status: Accepted!
Program Runtime: 4 milli secs
class Solution {
private:
//判断从start位置开始到end位置结束,s的字串是否为回文
bool ispalindrome(const string s,int start, int end){
// int len = s.length();
int i=start, j= end;
while(i<j)
{
if(s[i] != s[j])
return false;
i++;
j--;
}
return true;
}
public:
vector<vector<string> > partition(string s) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<vector<string> > result;
vector<string> tmp;
int len = s.length();
for(int i=len; i>0; i--)
{
if(ispalindrome(s, 0, i-1))
{
//s本身是回文,则将s入栈
if(i == len)
{
tmp.push_back(s.substr(0, i));
result.push_back(tmp);
tmp.clear();
}
else{
vector<vector<string> > p = partition(s.substr(i, len - i));
vector<vector<string> >::iterator it = p.begin();
while(it != p.end())
{
vector<string>::iterator iq = (*it).begin();
tmp.push_back(s.substr(0, i));
while(iq!=(*it).end())
{
tmp.push_back(*iq);
iq++;
}
result.push_back(tmp);
tmp.clear();
it++;
}
}
}
}
return result;
}
};
- LeetCode之Palindrome Partitioning
- leetcode之Palindrome Partitioning
- leetcode之Palindrome Partitioning
- leetcode之Palindrome Partitioning
- LeetCode之Palindrome Partitioning
- leetcode之 Palindrome Partitioning
- LeetCode之Palindrome Partitioning II
- leetcode之Palindrome Partitioning II
- LeetCode之Palindrome Partitioning II
- LeetCode之Palindrome Partitioning II
- leetcode之 Palindrome Partitioning I&II
- leetcode之 Palindrome Partitioning I&II
- leetcode之 Palindrome Partitioning I&II
- 【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
- UVa 729 - The Hamming Distance Problem
- oracle user-managed backups
- 异常org.mybatis.spring.transaction.SpringManagedTransactionFactory.newTransaction
- 当输入3时输出:1+2,当输入15时输出:1+2+3+4+5;4+5+6;7+8
- 新手慎用最新版的Fedora
- LeetCode之Palindrome Partitioning
- postgresql pgbench
- C# Main 里面参数的含义
- [转]Android签名机制
- 牢骚&总结 of ACM
- oracle user-maneged recovery(一)
- sql newid()随机函数
- c# 线程,不卡界面
- C++ 标准模板库STL set 使用方法与应用介绍(三)