-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpartitionPalindrome.h
64 lines (56 loc) · 1.52 KB
/
partitionPalindrome.h
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
* partitionPalindrome.h
*
* Created on: 2019年4月11日
* Author: yindong
*/
#ifndef PARTITIONPALINDROME_H_
#define PARTITIONPALINDROME_H_
#include<vector>
#include<string>
using namespace std;
class PartitionPalindrome{
public:
vector<vector<string>> partition(string s) {
vector<vector<string>> results;
vector<string> res;
partition_(s, 0, res, results);
return results;
}
int partition_(const string &s, int ind, vector<string> res, vector<vector<string>> &results){
// 停止条件
if (ind == s.size()){
results.push_back(res);
}
// 递归过程
for(int i = 1; i <= s.size() - ind; i++ ){
if(isPalindrome(s.substr(ind, i))){
res.push_back(s.substr(ind, i));
partition_(s, ind + i, res, results);
res.pop_back();
}
}
return 1;
}
pa
bool isPalindrome(string s) {
if (s.size() == 0)
return true;
string s_st;
for (unsigned int i = 0; i < s.size(); i++){
if (isalnum(s[i])){
s_st.push_back(tolower(s[i]));
// cout<< s[i] << endl;
}
}
// cout << "new string: " << s_st << endl;
int left = 0, right = s_st.size() - 1;
while(left < right){
// cout << '\t' << left << ": " << s[left] << '\t' << right << ": " << s[right] << endl;
if ( not ( s_st[left++] == s_st[right--] ) )
return false;
}
return true;
}
};
#endif /* PARTITIONPALINDROME_H_ */