首页 > 代码库 > Palindrome Partitioning
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"] ]解题思路:很简单的一题判定回文的题,利用递归调用的方式输出每种可能.
#include<iostream> #include<vector> #include<string> using namespace std; bool JudgeIsPalindrome(string s) { for (int first = 0, last = s.size() - 1; first <= last;++first,--last){ if (s[first] == s[last]) continue; else return false; } return true; } void RecurisonSplit(string &S, vector<string> &Result, vector<vector<string> >&ResultVector) { if (S.empty()){ ResultVector.push_back(Result); return; } for (int len = 1; len <= S.size();++len) { if (JudgeIsPalindrome(S.substr(0, len))){ Result.push_back(S.substr(0, len)); RecurisonSplit(S.substr(len), Result, ResultVector); Result.pop_back(); } } } vector<vector<string>> partition(string s) { vector<vector<string> >ResultVector; vector<string>Result; RecurisonSplit(s, Result, ResultVector); return ResultVector; }
Palindrome Partitioning
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。