-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path131.cpp
35 lines (31 loc) · 929 Bytes
/
131.cpp
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
class Solution {
public:
vector<vector<string>> partition(string s) {
vector<vector<string> > ret;
if(s.empty()) return ret;
vector<string> path;
dfs(0, s, path, ret);
return ret;
}
void dfs(int index, string& s, vector<string>& path, vector<vector<string> >& ret) {
if(index == s.size()) {
ret.push_back(path);
return;
}
for(int i = index; i < s.size(); ++i) {
if(isPalindrome(s, index, i)) {
path.push_back(s.substr(index, i - index + 1));
dfs(i+1, s, path, ret);
path.pop_back();
}
}
}
// 判断是否是回文
bool isPalindrome(const string& s, int start, int end) {
while(start <= end) {
if(s[start++] != s[end--])
return false;
}
return true;
}
};