Problem I : Palindrome Partitioning I
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"]
]
class Solution {
public:
vector<vector<string>> partition(string s)
{
vector<vector<string>> res;
vector<string> tmp;
dfs(res,tmp,s);
return res;
}
void dfs(vector<vector<string>> &res,vector<string> &tmp,string s)
{//截成多少段
if(s.length()==0)
{
res.push_back(tmp);
return ;
}
for(int i=1;i<=s.length();i++)
{
//md 理解substr函数吗
string sub_str=s.substr(0,i);
if(isvaild(sub_str))
{
tmp.push_back(sub_str);
dfs(res,tmp,s.substr(i));
tmp.pop_back();
}
}
}
bool isvaild(string s)
{
int i=0;
int j=s.length()-1;
while(i<j)
{
if(s.at(i++)!=s.at(j--)) return false;
}
return true;
}
};
1 dfs 理解
2 字串substr函数
3 回文数字