LeetCode131: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”]
]
最開始看到这道题时毫无思路。可能是看到回文就怕了。也想不出怎样用回溯来求解。
于是在纸上随便乱画了一些,结果发现好像能够依照这个思路求解了,简直囧啊。
对于上面的”aab”作为输入。能够这么寻找回文:
“a”+”ab”构成的回文串
“aa”+”b”构成的回文串
“aab”不是回文。所以直接退出。
于是感觉对于一个字符串,能够对这个字符串进行遍历,假设前pos个字符串本身是个回文字符。那么仅仅须要求解后面的子字符的回文串就可以,于是这个问题被分解成了一个更小的问题。
这道题更像一个分治法的题,将问题规模不断缩小,当然的遍历字符串的过程中须要进行回溯。
除了须要一个进行递归的辅助函数外。还须要定义一个推断一个字符串是否是回文字符串的辅助函数。程序的逻辑很easy。
这道题和Combination Sum 比較相似,一開始看到这道题时全然感觉无从下手,可是在纸上写几个測试用例,从特殊的測试用例中就能够发现规律了。加上回溯后的递归都没有那么一目了然,可能有測试用例会更easy懂一些。
runtime:20ms
class Solution {
public:vector<vector<string>> partition(string s) {vector<string> path;vector<vector<string>> result;helper(s,0,path,result);return result;}void helper(string s,int pos,vector<string> & path,vector<vector<string>> & result){if(pos==s.size()){result.push_back(path);return ;}for(int i=pos;i<s.size();i++){if(isPalindrome(s.substr(pos,i-pos+1))){path.push_back(s.substr(pos,i-pos+1));helper(s,i+1,path,result);path.pop_back();}}}bool isPalindrome(string s){int first=0;int end=s.size()-1;while(first<end){if(s[first++]!=s[end--])return false;}return true;}
};
转载于:https://www.cnblogs.com/gccbuaa/p/7392227.html
LeetCode131:Palindrome Partitioning相关推荐
- 【LeetCode】Palindrome Partitioning 解题报告
[题目] Given a string s, partition s such that every substring of the partition is a palindrome. Retur ...
- Lintcode108 Palindrome Partitioning || solution 题解
[题目描述] Given a strings, cutsinto some substrings such that every substring is a palindrome. Return t ...
- 【leetcode】132. Palindrome Partitioning II
题目如下: 解题思路:本题是[leetcode]131. Palindrome Partitioning的升级版,要求的是求出最小cuts,如果用[leetcode]131. Palindrome P ...
- Palindrome Partitioning
题目:Given a string s, partition s such that every substring of the partition is a palindrome. Return ...
- C++palindrome partitioning回文分割算法的实现(附完整源码)
C++palindrome partitioning回文分割算法的实现 C++palindrome partitioning回文分割算法的实现的完整源码(定义,实现,main函数测试) C++pali ...
- [Lintcode]136. Palindrome Partitioning /[Leetcode]131. Palindrome Partitioning
136. Palindrome Partitioning / 131. Palindrome Partitioning 本题难度: Medium Topic: Search DFS Descripti ...
- [Leetcode Week13]Palindrome Partitioning
Palindrome Partitioning 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/palindrome-partitioning/desc ...
- LeetCode Palindrome Partitioning II
原题链接在这里:https://leetcode.com/problems/palindrome-partitioning-ii/ 题目: Given a string s, partition s ...
- 【回文串5 重点+动态规划】LeetCode 132. Palindrome Partitioning II
LeetCode 132. Palindrome Partitioning II Solution1:我的答案1 直接模仿131那道题的DFS解法,找其中size最小的.果不其然,因为超时只能部分AC ...
- 【回文串4 DFS】LeetCode 131. Palindrome Partitioning
LeetCode 131. Palindrome Partitioning DFS的经典套路题目!!! 八皇后问题写法类似!!! Solution1: 转载网址:http://www.cnblogs. ...
最新文章
- 忍不住要发感慨,我的这个罗技鼠真好用...
- transforms.normalize()函数
- Android --- java.lang.RuntimeException: Can‘t create handler inside thread that has not called Loop
- 在浏览器输入一个网址,按回车之后发生了什么?
- iBatis.Net(C#)数据库查询
- LeetCode 1877. 数组中最大数对和的最小值(贪心)
- Controller数据导出Excel 详细教程——easypoi-base,easypoi-web,easypoi-annotation
- 转 Procrastination
- Eclipse插件开发中对于外部Jar包和类文件引用的处理(彻底解决插件开发中的NoClassDefFoundError问题)...
- Java中获取资源文件路径
- ELKElasticSearch5.1基础概念及配置文件详解【转】
- JAVA多线程(转)
- 五招查出想要知道的IP地址
- vue-项目完成的项目报告
- findbug规则说明
- 没有密码怎么解除PDF加密
- python弹幕点歌_GitHub - smilecc/blive-raspberry: 完全重构的树莓派B站直播弹幕点播台...
- Windows如何根据代码签名生态系统确定要信任的软件
- ArcGIS 移动、旋转、比例缩放、复制工具
- L1-7 机工士姆斯塔迪奥
热门文章
- libcurl学习及简易封装类
- maven 不能设置为web2.5的解决方法
- Linux的append函数,linux block层
- php 拷贝文件至新目录,如何查找到文件以后,带目录一起拷贝到新的目录? cp --parents source destination...
- python 获取当前路径_Python获取当前路径实现代码
- 华中师范大学计算机学院郑,郑年亨-华中师范国家数字化研究中心
- go 中 = 与:= 区别
- 手机联系人分组名字都没了_艺术签名手写设计,每个名字都应该是精品,只是你没找对方法...
- WPS如何并排放置两张图片_Animate如何制作文字动图动画
- 【渝粤教育】国家开放大学2018年秋季 0570-21T摄像、录像制作 参考试题