泰州市网站制作公司,济南58同城网,佛山外贸网站建设精英,宣传册设计样本题目链接#xff1a;131. 分割回文串 - 力扣#xff08;LeetCode#xff09;
要找出所有分割这个字符串的方案使得每个子串都是回文串#xff0c;写一个判断回文串的函数#xff0c;深度遍历回溯去找出所有分割方案#xff0c;判断分割的子串是否是回文串
class Soluti…题目链接131. 分割回文串 - 力扣LeetCode
要找出所有分割这个字符串的方案使得每个子串都是回文串写一个判断回文串的函数深度遍历回溯去找出所有分割方案判断分割的子串是否是回文串
class Solution {
public:vectorvectorstring ans;vectorstring an;string s;bool isPalindromes(int left, int right) {while (left right) {if (s[left] ! s[right--])return false;}return true;}void dfs(int i) {if (i s.size()) {ans.push_back(an);return;}for (int j i; j s.size(); j) {if (isPalindromes(i, j)) {an.push_back(s.substr(i, j - i 1));dfs(j 1);an.pop_back();}}}vectorvectorstring partition(string s) {this-s move(s);dfs(0);return ans;}
};