Palindrome Partitioning
2014-04-04 12:44
169 查看
题目:
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 =
Return
算法思想:
1,递归写法:从首字符k开始,找到字符串s中下个k,判断以两字符为首尾的子字符串A是否为回文,如果是的话用余下的字符串B作为参数传递给递归函数,然后将A插入到递归函数返回的结果中;如果不是的话继续寻找直至到字符串末尾。
2,迭代写法:这个可以在构建出IsPal[][]的基础上得到,参见点击打开链接。
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,递归写法:从首字符k开始,找到字符串s中下个k,判断以两字符为首尾的子字符串A是否为回文,如果是的话用余下的字符串B作为参数传递给递归函数,然后将A插入到递归函数返回的结果中;如果不是的话继续寻找直至到字符串末尾。
2,迭代写法:这个可以在构建出IsPal[][]的基础上得到,参见点击打开链接。
vector> partition(string s) { vector> strss; if (s.length() == 0) return strss; else if (s.length() == 1) { strss.push_back(vector(1, s)); return strss; } else { char c = s[0]; for (int i = 0; i < s.length(); i++) { i = s.find_first_of(c, i); if (i == -1) break; string s_temp1 = s.substr(0, i+1); string s_temp2(s_temp1.rbegin(), s_temp1.rend()); if (s_temp1 == s_temp2) { vector> strss_temp = partition(s.substr(i+1, s.length()-1)); if (strss_temp.size() == 0) { strss.push_back(vector(1, s_temp1)); } else { for (int j = 0; j < strss_temp.size(); j++) { strss_temp[j].insert(strss_temp[j].begin(), s_temp1); strss.push_back(strss_temp[j]); } } } } return strss; } }
相关文章推荐
- Palindrome Partitioning II
- palindrome-partitioning Java code
- leetcode---palindrome-partitioning---dp, dfs
- 132. Palindrome Partitioning II
- [LeetCode]Palindrome Partitioning II
- 在线编程:Palindrome Partitioning II
- [leetcode刷题系列]Palindrome Partitioning II
- [leetcode] Palindrome Partitioning II
- LeetCode "Palindrome Partitioning"算法时间复杂度分析
- [leetcode]Palindrome Partitioning II
- Palindrome Partitioning II
- leetcode Palindrome Partitioning II
- 【LeetCode】Palindrome Partitioning I && II
- Leetcode:Palindrome Partitioning II
- Palindrome Partitioning II
- Leetcode_Palindrome_Partitioning_DFS 算法
- Palindrome Partitioning I and II leetcode
- Palindrome Partitioning II
- Leetcode: Palindrome Partitioning II
- LeetCode Palindrome Partitioning I&&Palindrome Partitioning II