-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path131.cpp
More file actions
48 lines (43 loc) · 1.06 KB
/
Copy path131.cpp
File metadata and controls
48 lines (43 loc) · 1.06 KB
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
38
39
40
41
42
43
44
45
46
47
48
#include<iostream>
#include<string>
#include<vector>
using namespace std;
class Solution {
public:
vector<vector<string> > partition(string s) {
int size = s.size();
if(size <= 0)
return result;
dfs(s, size, 0);
return result;
}
void dfs(string str, int size, int start) {
if(start == size) {
result.push_back(path);
return;
}
for(int i = start; i < size; ++i) {
string t = str.substr(start, i-start+1);
if(IsPalindrome(t)) {
path.push_back(t);
dfs(str, size, i+1);
path.pop_back();
}
}
}
/// 判断字符串是否是回文串
bool IsPalindrome(string str) {
int size = str.size();
if(size == 0)
return false;
for(int i=0, j=size-1; i<j; i++, j--)
if(str[i] != str[j])
return false;
return true;
}
private:
vector<string> path;///中间结果
vector<vector<string> > result; ///最终结果
};
int main() {
}