leetcode—Palindrome 解题报告
1.题目描述
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"]
]
也就是给定一个字符串,找出所有可能的划分,每一个划分的每一个字串都是回文串。设这个划分函数为partition_index(string s,int begin,int end)
2.解题思路
蛮力法当然是可以解决这个问题,对于一个长度为n的字符串,有2n-1种划分方法,只需要判断每种划分是否满足条件即可,虽然不知道暴力法能不能在系统里面ac掉,但是这样显然不是一个正常的程序员该干的事。不过蛮力法会给我们一些启示。我们从为什么蛮力法会有2n-1种划分方法开始分析。
当然,这也很容易分析,对于长度为n的字符串s0s1…sk…sn-1,在sk和sk-1之间我们可以选择截断与不截断,但是,我们可以基于这个思路理出一个递归的思路,那就是,对于字符串sbeginsbegin+1…sbegin+k…sn-1,所有可能满足条件的划分分为两类:
(1)在sbegin和sbegin+1之间有隔断,这种情况,只需sbegin和partition_index(s,begin+1,n-1)连接起来即可
(2)在sbegin和sbegin+1之间无隔断,这种情况,需找到至少包含sbegin和sbegin+1的一个回文字符串,如果能够找到,比如,sbegin…sbegin+k是一个回文字符串,那么,将这个回文字符串和partition_index(s,k+1,n-1)连接起来,注意,可能找到不止一个包含s0和s1的回文字符串,那么有多少个,就多出多少类划分可能;如果找不到,那么返回空vector.
3.代码
#include <iostream>#include <vector>#include <string>#include <iterator>using namespace std;vector<vector<string> > partition(string s);vector<vector<string> > partition_index(string s, int begin, int end);bool isPalindrome(string s, int begin,int end);int main(){partition("cdd");return 0;}vector<vector<string> > partition(string s) {return partition_index(s,0,s.length()-1);}vector<vector<string> > partition_index(string s, int begin, int end){vector<vector<string> > result;//边界条件if(begin == end){vector<string> item;item.push_back(s.substr(begin,1));result.push_back(item);return result;}if(begin > end) return result;//如果在begin与begin+1之间有划分vector<vector<string> > result_part1;result_part1 = partition_index(s,begin+1,end);if(!result_part1.empty()){vector<vector<string> >::iterator iter_vv;for(iter_vv = result_part1.begin();iter_vv!=result_part1.end();++iter_vv){vector<string> temp;//temp = *iter_vv;temp.push_back(s.substr(begin,1));vector<string>::iterator iter_v;for(iter_v = (*iter_vv).begin();iter_v!= (*iter_vv).end();++iter_v){temp.push_back(*iter_v);}result.push_back(temp);}}//如果在begin和begin+1之间无划分int nextSeg = begin +1;while(nextSeg<=end){//找到了回文字符串if(isPalindrome(s,begin,nextSeg)){vector<vector<string> >result_part2_1;result_part2_1 = partition_index(s,nextSeg+1,end);if(!result_part2_1.empty()){vector<vector<string> >::iterator iter_vv;for(iter_vv = result_part2_1.begin();iter_vv!=result_part2_1.end();++iter_vv){vector<string> temp;// temp = *iter_vv;temp.push_back(s.substr(begin,nextSeg));vector<string>::iterator iter_v;for(iter_v = (*iter_vv).begin();iter_v!= (*iter_vv).end();++iter_v){temp.push_back(*iter_v);}result.push_back(temp);}}else{vector<string>temp;temp.push_back(s.substr(begin,nextSeg-begin+2));result.push_back(temp);}}//继续找看是否有其他的更长的回文字符串nextSeg++;}return result;}//判断字符串由begin 和end 确定的字串是否为回文字符串bool isPalindrome(string s, int begin,int end){if(begin>end)return false;if(begin==end)return true;if((begin+1)==end)return s[begin]==s[end];elseif(s[begin]==s[end])return isPalindrome(s,begin+1,end-1);esle return false;}
另外,代码可以简化,比如,连接结果的代码可以封装成一个函数.
leetcode—Palindrome 解题报告的更多相关文章
- LeetCode: Permutations 解题报告
Permutations Given a collection of numbers, return all possible permutations. For example,[1,2,3] ha ...
- 【LeetCode】409. Longest Palindrome 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:字典统计次数 方法二:HashSet 方法三 ...
- 【LeetCode】214. Shortest Palindrome 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 前缀是否回文 判断前缀 相似题目 参考资料 日期 题 ...
- 【LeetCode】125. Valid Palindrome 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 列表生成式 正则表达式 双指针 日期 题目地址:https:/ ...
- LeetCode: Valid Palindrome 解题报告
Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric char ...
- LeetCode C++ 解题报告
自己做得LeetCode的题解,使用C++语言. 说明:大多数自己做得,部分参考别人的思路,仅供参考; GitHub地址:https://github.com/amazingyyc/The-Solut ...
- C++版 - 剑指offer之面试题37:两个链表的第一个公共结点[LeetCode 160] 解题报告
剑指offer之面试题37 两个链表的第一个公共结点 提交网址: http://www.nowcoder.com/practice/6ab1d9a29e88450685099d45c9e31e46?t ...
- LeetCode: Subsets 解题报告
Subsets Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset ...
- LeetCode: Triangle 解题报告
Triangle Given a triangle, find the minimum path sum from top to bottom. Each step you may move to a ...
随机推荐
- PAT-乙级-1027. 打印沙漏(20)
1027. 打印沙漏(20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 本题要求你写个程序把给定的符号打印成 ...
- 设置UINavigation的背景图片和背景颜色
//通过背景图片来设置背景 float systemVersion = [[[UIDevice currentDevice] systemVersion] floatValue]; UIImage * ...
- KafkaSpout: PartitionManager的行为分析
KafkaSpout的核心逻辑都是由PartitionManager来实现的. 但是这个类实现时候需要考虑的东西有些多,0.92至0.93,至当前(2015.3.14)的master一直在变化.在这里 ...
- 极客范:如何使用 Cloud Insight 来监控闭路电视?
最近新上线支持 Windows 系统及其组件 监控功能的 Cloud Insight,在系统监控领域基本囊括了对所有主流和部分非主流平台的支持.但是这还不够,Cloud Insight 可不仅仅是一个 ...
- cocos2d-x 常规库的图文件配置
LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS) LOCAL_MODULE := cocos_lua_static LOCAL_MODULE_FILE ...
- Android:Resources资源文件
Android Resoureces是res目录下的那些目录和文件,常用的有: res/drawable/ 存放图片资源,类型有: 相关使用: Android:res之shape制作圆角 Androi ...
- 最短路径算法之一——Floyd算法
Floyd算法 Floyd算法可以用来解决任意两个顶点之间的最短路径问题. 核心公式为: Edge[i][j]=Min{Edge[i][j],Edge[i][k]+Edge[k][j]}. 即通过对i ...
- HDU4614【线段树。】
果然看了理解了一下大牛的代码然后自己敲结果果然有不少错误 回复说,线段树做为一种数据结构,最好以一种风格过一题裸的然后作为自己的模板.. 二分写的也很恶心哪 还有题目稍复杂一点的注定得推敲各种公式,不 ...
- python学习笔记六--用户自定义类
一.类: 1. 面向对象. 2. 定义了新的对象类型. 定义了两个属性:name,pay 定义了两个方法:lastName,giveRaise
- 【HDOJ】4729 An Easy Problem for Elfness
其实是求树上的路径间的数据第K大的题目.果断主席树 + LCA.初始流量是这条路径上的最小值.若a<=b,显然直接为s->t建立pipe可以使流量最优:否则,对[0, 10**4]二分得到 ...