首页 > 代码库 > [LeetCode] Palindrome Partitioning
[LeetCode] Palindrome Partitioning
Given a string s, partition s such that every substring of the partition is a palindrome.
Return all possible palindrome partitioning of s.
For example, given s = "aab"
, Return
[ ["aa","b"], ["a","a","b"] ]
注意下面的例子:
Input: | "fff" |
Expected: | [["f","f","f"],["f","ff"],["ff","f"],["fff"]] |
class Solution {public: vector<vector<string>> partition(string s) { vector<vector<string>> res; if(s.size()<1) return res; vector<string> partition; addPalindrome(s,0,partition,res); return res; }private: void addPalindrome(string s,int start,vector<string> &partition,vector<vector<string> >&res){ if(start == s.size()){ res.push_back(partition); return; } for(int i = start+1;i<=s.size();i++){ string str = s.substr(start,i-start); if(isPalindrome(str)){ partition.push_back(str); addPalindrome(s,i,partition,res); partition.pop_back(); }//end if }//end for } bool isPalindrome(string str){ int left = 0; int right = str.size()-1; while(left<right){ if(str[left]!=str[right]) return false; left++; right--; }//end while return true; }};
方法:DFS!
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。