131. Palindrome Partitioning回溯算法
来源:互联网 发布:2016网络十大洗脑神曲 编辑:程序博客网 时间:2024/05/17 22:51
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"] ]
Subscribe to see which companies asked this question
分析:
回溯算法,每次增长一个长度检验是否为回文数。
经典解法。
class Solution {
public:
bool check(string s)
{
int i=0;
int j=s.size()-1;
while(i<j)
{if(s[i]!=s[j]) return false;++i;--j;}
return true;
}
void build(string s,int t,vector<vector<string>> &v1,vector<string> v2)
{
if(t==s.size()){v1.push_back(v2);}
else
{
string s1="";
for(int i=t;i<s.size();++i)
{
s1+=s[i];
v2.push_back(s1);
if(check(s1)) {build(s,i+1,v1,v2);}
v2.pop_back();
}
}
}
vector<vector<string>> partition(string s) {
vector<vector<string>> v1;
vector<string> v2;
if(s.size()==0) return v1;
string s1="";
for(int i=0;i<s.size();++i)
{
s1+=s[i];
v2.push_back(s1);
if(check(s1)) { build(s,i+1,v1,v2);}
v2.pop_back();
}
return v1;
}
};
- 131. Palindrome Partitioning回溯算法
- leetcode 131. Palindrome Partitioning-回溯算法
- 131. Palindrome Partitioning(字符串,dfs回溯)
- LeetCode 131. Palindrome Partitioning--回溯法
- LeetCode 131. Palindrome Partitioning 树形问题,状态回溯
- [leetcode][回溯][DP] Palindrome Partitioning //TODO
- 「回溯法」Palindrome Partitioning
- leetcode---Palindrome Partitioning---动规、回溯
- 131. Palindrome Partitioning && 132. Palindrome Partitioning II
- LeetCode 131. Palindrome Partitioning
- [LeetCode]131.Palindrome Partitioning
- [Leetcode] 131. Palindrome Partitioning
- [leetcode] 131.Palindrome Partitioning
- 131. Palindrome Partitioning
- 131. Palindrome Partitioning
- 131. Palindrome Partitioning
- [LeetCode]131. Palindrome Partitioning
- 131. Palindrome Partitioning LeetCode
- 我说《古炉》之古炉村
- Activity的生命周期
- iOS 百度地图 设置显示区域 中心点和范围
- movsx汇编指令说明
- 飛飛(十五)设计一个小学生加减乘除的十道随机题
- 131. Palindrome Partitioning回溯算法
- 1087. All Roads Lead to Rome (30)
- 剑指offer-面试题25:二叉树中和为某一值的路径
- poj 3009 curling
- SDAU课程练习2 1010
- DMA驱动
- 【千里码】老王装货 最优化问题
- Android 设备唯一标识生成方式
- 92. Reverse Linked List II