首页 > 代码库 > leetcode 131. Palindrome Partitioning
leetcode 131. 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"] ]
1 vector <vector<string> > results; 2 vector <string> line; 3 vector<vector<string>> partition(string s) 4 { 5 getPartition(s, 0); 6 return results; 7 } 8 9 void getPartition(string s, int index)10 {11 if (index >= s.size())12 {13 results.push_back(line);14 return;15 }16 17 for (int i = index; i < s.size(); i++)18 {19 string ts = s.substr(index, i + 1 - index);20 if (isPalindrome(ts))21 {22 line.push_back(ts);23 getPartition(s, i + 1);24 line.pop_back();25 }26 }27 }28 29 bool inline isPalindrome(string s)30 {31 int i = 0, j = s.size() - 1;32 while (i < j)33 {34 if (s[i] == s[j])35 i++, j--;36 else37 return false;38 }39 40 return true;41 }
leetcode 131. Palindrome Partitioning
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。