首页 > 代码库 > 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"] ]
class Solution { public: std::vector<std::vector<std::string>> partition(std::string s) { std::vector<std::vector<std::string>> result; std::vector<std::string> ans; dfs(s,ans,result); #if 0 for (int i = 0; i < result.size(); i++) { for (int j = 0; j < result[i].size(); j++) { std::cout << result[i][j] << " "; } std::cout << std::endl; } #endif // 1 return result; } private: void dfs(std::string s,std::vector<std::string> &ans,std::vector<std::vector<std::string>> &result) { if(s.size() < 1) { result.push_back(ans); return ; } for (int i = 0; i < s.size(); i++) { int a = 0,b = i; while(a < b) { if(s[a] == s[b]) a++,b--; else break; } if(a >= b) { ans.push_back(s.substr(0,i+1)); dfs(s.substr(i+1),ans,result); ans.pop_back(); } } } };
leetcode - Palindrome Partitioning
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。