class Solution {
public:
vector<vector<string>> res;
vector<string> path;
vector<vector<string>> partition(string s) {
f(s, 0);
return res;
}
void f(string s, int start) {
if (start == s.size()) {
res.push_back(path);
return;
}
for (int idx = start; idx < s.size(); ++idx) {
string sub = s.substr(start, idx-start + 1);
if (isPalindrome(sub)) {
path.push_back(sub);
f(s, idx +1);
path.pop_back();
}
}
}
bool isPalindrome(string x) {
int l=0, r = x.size()-1;
while (l < r) {
if (x[l] != x[r]) return false;
l++;r--;
}
return true;
}
};
LeetCode 131. 分割回文串
最新推荐文章于 2024-07-25 21:13:04 发布