[LeetCode] 064: Palindrome Partition
来源:互联网 发布:js点击按钮让日期增加 编辑:程序博客网 时间:2024/06/08 02:54
[Problem]
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"] ][Solution]
class Solution {说明:版权所有,转载请注明出处。Coder007的博客
public:
/**
* is string s a palindrome string
*/
bool isPalindrome(string s){
if(s.size() == 0)return true;
int i = 0, j = s.size()-1;
while(i < j){
if(s[i] != s[j]){
return false;
}
i++;
j--;
}
return true;
}
/**
* partition
*/
vector<vector<string> > partition(string s) {
// Note: The Solution object is instantiated only once and is reused by each test case.
vector<vector<string> > res;
// less than 2 characters
if(s.size() <= 1){
vector<string> row;
row.push_back(s);
res.push_back(row);
}
else{
// partition
for(int i = 1; i <= s.size(); ++i){
string head = s.substr(0, i);
if(!isPalindrome(head))continue;
// end of partition
if(i == s.size()){
vector<string> row;
row.push_back(head);
res.push_back(row);
}
else{
vector<vector<string> > nextRes = partition(s.substr(i, s.size()-i));
for(int j = 0; j < nextRes.size(); ++j){
nextRes[j].insert(nextRes[j].begin(), head);
res.push_back(nextRes[j]);
}
}
}
}
return res;
}
};
阅读全文
0 0
- [LeetCode] 064: Palindrome Partition
- LeetCode 131: Palindrome Partition
- [LeetCode] Palindrome Partition
- [LeetCode] Palindrome Partition [11]
- LeetCode: Palindrome Partition
- leetcode palindrome partition II
- LeetCode——Palindrome Partition
- LeetCode —— Palindrome Partition II
- Palindrome Partition
- palindrome partition II
- palindrome-partition-ii
- LeetCode :: Palindrome
- LeetCode(Palindrome partition 2) 求将一个字符串划分成回文子串 需要分成的段数最少是多少
- Two pointers (2) -- Partition List,Valid Palindrome
- LeetCode: Partition List
- LeetCode Partition List
- LeetCode: Partition List
- [Leetcode] Partition List **
- [LeetCode] 062: Next Permutation
- ConcurrentHashMap实现原理
- [LeetCode] 063: Palindrome Number
- httpclient_get_post
- 【转】简介ajax
- [LeetCode] 064: Palindrome Partition
- [LeetCode] 065: Palindrome Partitioning II
- [LeetCode] 066: Partition List
- [LeetCode] 067: Pascal\'s Triangle
- [LeetCode] 068: Pascal\'s Triangle II
- [LeetCode] 069: Path Sum
- 国外物联网平台初探(一) ——亚马逊AWS IoT
- 今日头条笔试题
- [LeetCode] 070: Path Sum II