Palindrome Partitioning问题及解法
来源:互联网 发布:矩阵的计算方法 编辑:程序博客网 时间:2024/05/18 18:43
问题描述:
Given a string s, partition s such that every substring of the partition is a palindrome.
Return all possible palindrome partitioning of s.
示例, given s = "aab"
,
Return
[ ["aa","b"], ["a","a","b"]]
问题分析:
此类问题属于回溯法范畴,对每次取出的子串判断是否是回文的,若全都是,则把这些字串放入到res中,否则不作处理。
过程详见代码:
class Solution {public: vector<vector<string>> partition(string s) {vector<vector<string>>res;vector<string> re;bl(res,re,0,s,s.length()); return res;}void bl(vector<vector<string>>&res, vector<string>& re, int start, string s,int left){if (!left){res.push_back(re);return;}for (int i = 1; i <= left; i++){string t = s.substr(start, i);if (!palindrome(t)) continue;re.push_back(t);bl(res, re, start + i, s, left - i);re.pop_back();}}bool palindrome(string s){for (int i = 0; i < s.length() / 2; i++){if (s[i] != s[s.length() - 1 - i]) return false;}return true;}};
阅读全文
0 0
- Palindrome Partitioning问题及解法
- Palindrome Number问题及解法
- Valid Palindrome问题及解法
- Longest Palindrome问题及解法
- leetcode: Palindrome Partitioning II解法
- Palindrome Linked List问题及解法
- Largest Palindrome Product问题及解法
- Valid Palindrome II问题及解法
- 【LeetCode】132. Palindrome Partitioning II 基于动态规划DP、C++、Java的分析及解法
- Palindrome Partitioning I,II[leetcode] DFS以及DP的解法
- Palindrome partitioning
- Palindrome Partitioning
- Palindrome Partitioning
- Palindrome Partitioning
- Palindrome Partitioning
- Palindrome Partitioning
- Palindrome Partitioning
- Palindrome Partitioning
- OpenJudge 8469: 特殊密码锁(贪心)
- Nginx之旅:安装
- 浅谈JVM垃圾回收机制
- acm中的二进制操作(未完成)
- Leetcode 142. Linked List Cycle II
- Palindrome Partitioning问题及解法
- Android6.0动态获取权限java.io.FileNotFoundException: …Permission denied
- 留言板HTML文件
- Android应用关闭系统软键盘
- 回归分析
- jdk版本错误的解决办法
- 序列化、字节对齐、字节序(C++)
- 小白也能看懂的插件化DroidPlugin原理(二)-- 反射机制和Hook入门
- (CSU-1976)搬运工小明