给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串。
返回 s 所有可能的分割方案。
例如,给出 s = "aab",
返回
[
["aa","b"],
["a","a","b"]
]
详见:https://leetcode.com/problems/palindrome-partitioning/description/
class Solution { public: vector<vector<string>> partition(string s) { vector<vector<string>> res; vector<string> out; if(s.size()==0||s.empty()) { return res; } helper(s,0,out,res); return res; } void helper(string &s,int start,vector<string> &out,vector<vector<string>> &res) { if(start==s.size()) { res.push_back(out); return; } for(int i=start;i<s.size();++i) { if(isPalindrome(s,start,i)) { out.push_back(s.substr(start,i-start+1)); helper(s,i+1,out,res); out.pop_back(); } } } bool isPalindrome(string &s,int start,int end) { while(start<end) { if(s[start]!=s[end]) { return false; } ++start; --end; } return true; } };
原文地址:https://www.cnblogs.com/xidian2014/p/8723594.html
时间: 2024-10-28 17:22:47