题目:
代码(首刷看解析 2024年2月3日):
class Solution {
public:vector<vector<string>> res;vector<string> path;bool isPalindrome(const string& s, int start, int end) {for (int i = start, j = end; i < j; i++, j--) {if (s[i] != s[j]) {return false;}}return true;}void backtracking(const string& s, int startIndex) {if (startIndex >= s.size()) {res.push_back(path);return;}for (int i = startIndex; i < s.size(); ++i) {if (isPalindrome(s, startIndex, i)) {string str = s.substr(startIndex, i + 1 - startIndex);path.push_back(str);} else {continue;}backtracking(s, i + 1);path.pop_back();}return;}vector<vector<string>> partition(string s) {if (s.size() == 0) return res;backtracking(s, 0);return res;}
};