题解 | #所有的回文子串II#
所有的回文子串II
https://www.nowcoder.com/practice/3373d8924d0e441987650194347d3c53
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param s string字符串 * @return string字符串vector */ bool isPalindrome(string& s, int start, int end) { int left = start, right = end; while (left < right) { if (s[left++] != s[right--]) return false; } return true; } void dfs(string& s, int start, vector<string> & result) { if (start == s.size()-1) return; for (int end = start+1; end < s.size(); end++) { if (isPalindrome(s, start, end)) { result.push_back(s.substr(start, end-start+1)); } } dfs(s, start+1, result); } vector<string> partitionII(string s) { // write code here vector<string> result; dfs(s, 0, result); // 使用unique之前需要先排序 sort(result.begin(), result.end()); result.erase(unique(result.begin(), result.end()), result.end()); return result; } };