-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy path131-分割回文串.cpp
More file actions
37 lines (37 loc) · 993 Bytes
/
131-分割回文串.cpp
File metadata and controls
37 lines (37 loc) · 993 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class Solution {
public:
bool isP(string& s,int start,int end) //判断是不是回文数
{
while(start < end)
{
if(s[start] != s[end])
return false;
start++;
end--;
}
return true;
}
void dfs(string& s, vector<string>& rec,vector<vector<string>>& result,int index)
{
if(index == s.size()) //遍历完
{
result.push_back(rec);
return;
}
for(int i = index;i<s.size();i++)
{
if( isP(s,index,i) ) //判断从index 到 i位置的是不是回文串
{
rec.push_back( s.substr(index,i - index + 1) );
dfs(s,rec,result,i + 1);
rec.pop_back(); //回溯
}
}
}
vector<vector<string>> partition(string s) {
vector<string> rec;
vector<vector<string>> result;
dfs(s,rec,result,0);
return result;
}
};