131. Palindrome Partitioning(回文子串划分 深度优先)
Return all possible palindrome partitioning of s.
For example, given s = "aab",
Return
[
["aa","b"],
["a","a","b"]
]
解题思路
求所有答案,首先排除动态规划,应该是DFS (Palindrome Partitioning II 求个数才是动归)
- 遇到要求所有组合、可能、排列等解集的题目,一般都是DFS + backtracking
- 首先传入s="aab" path=[] res = [], 首先切割出"a"(然后是"aa" "aab" ...),然后判读它是不是回文串:
- 如果不是,直接跳过
- 如果是,则此时剩余的 s="ab", path += ["a"]
- 写入res的判断是,当s=""时,记录结果
class Solution {
private List<List<String>> res = new ArrayList<>();
public List<List<String>> partition(String s) {
help(new ArrayList<>(), s, 0);
return res;
}
private void help( List<String> temp, String s, int index){
if(index == s.length())
res.add(new ArrayList<>(temp));
else{
for(int i = index; i < s.length(); i++){
if(isPalindrome(s, index, i)){
temp.add(s.substring(index, i + 1));
help(temp, s, i + 1);
temp.remove(temp.size() - 1);
}
}
}
}
public boolean isPalindrome(String s, int low, int high){
while(low < high)
if(s.charAt(low++) != s.charAt(high--)) return false;
return true;
}
}
131. Palindrome Partitioning(回文子串划分 深度优先)的更多相关文章
- [LeetCode] 131. Palindrome Partitioning 回文分割
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- Atcoder CODE FESTIVAL 2017 qual C D - Yet Another Palindrome Partitioning 回文串划分
题目链接 题意 给定一个字符串(长度\(\leq 2e5\)),将其划分成尽量少的段,使得每段内重新排列后可以成为一个回文串. 题解 分析 每段内重新排列后是一个回文串\(\rightarrow\)该 ...
- 【HDU】4632 Palindrome subsequence(回文子串的个数)
思路:设dp[i][j] 为i到j内回文子串的个数.先枚举所有字符串区间.再依据容斥原理. 那么状态转移方程为 dp[i][j] = dp[i][j-1] + dp[i+1][j] - dp[i+ ...
- Palindrome Partitioning (回文子串题)
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- 【算法】最长回文子串 longest palindrome substring
对于字符串S, 要找到它最长的回文子串,能想到的最暴力方法,应该是对于每个元素i-th都向左向右对称搜索,最后用一个数组span 记录下相对应元素i-th为中心的回文子串长度. 那么问题来了: 1. ...
- URAL 1297 Palindrome 最长回文子串
POJ上的,ZOJ上的OJ的最长回文子串数据量太大,用后缀数组的方法非常吃力,所以只能挑个数据量小点的试下,真要做可能还是得用manacher.贴一下代码 两个小错,一个是没弄懂string类的sub ...
- Palindrome - POJ 3974 (最长回文子串,Manacher模板)
题意:就是求一个串的最长回文子串....输出长度. 直接上代码吧,没什么好分析的了. 代码如下: ================================================= ...
- POJ 3974 Palindrome(最长回文子串)
题目链接:http://poj.org/problem?id=3974 题意:求一给定字符串最长回文子串的长度 思路:直接套模板manacher算法 code: #include <cstdio ...
- Ural 1297 Palindrome 【最长回文子串】
最长回文子串 相关资料: 1.暴力法 2.动态规划 3.中心扩展 4.Manacher法 http://blog.csdn.net/ywhorizen/article/details/6629268 ...
随机推荐
- (转)spring IOC、DI理解
转自: http://www.cnblogs.com/xdp-gacl/p/4249939.html 个人理解: IOC控制反转,反转的是获取依赖对象的方式.传统的应用在存在依赖关系时,比如A依赖于B ...
- 我学cocos2d-x (一) 游戏基本概念:坐标系与Anchor Point
坐标系: 游戏开发中.全部物体都有自己的位置,而我们须要一个參考系来描写叙述物体的位置.使用cocos2d-x开发的时候.有几个比較重要坐标系须要掌握:屏幕坐标系和Cocos2d坐标系 屏幕坐标系: ...
- 【RF库测试】Exit For Loop 相关
1.Exit For Loop If:满足条件时,跳出循环,后面的循环不再执行 2.Continue For Loop If:满足条件时,跳出本次循环,继续执行后面的循环
- 【BZOJ3555】[Ctsc2014]企鹅QQ hash
[BZOJ3555][Ctsc2014]企鹅QQ Description PenguinQQ是中国最大.最具影响力的SNS(Social Networking Services)网站,以实名制为基础, ...
- springMVC问题
网站中springmvc.xml配置: <bean id="viewResolver" class="org.springframework.web.servlet ...
- quartz启动Quartz : org.quartz.SchedulerConfigException: Thread count must be > 0
检查quartz.properties数据源配置是否正常
- springMVC的HandleMapping
http://blog.chinaunix.net/uid-20415521-id-1949916.html SpingMVC中的HandlerMapping (2007-05-22 11:33) 分 ...
- R中利用SQL语言读取数据框(sqldf库的使用)
熟悉MySQL的朋友可以使用sqldf来操作数据框 # 引入sqldf库(sqldf) library(sqldf) # 释放RMySQL库的加载(针对sqldf报错) #detach("p ...
- CentOS中制作本地yum源
1.光盘指向镜像 2.将镜像挂载到某个目录 mkdir /mnt/cdrom mount -t iso9660 -o ro /dev/cdrom /mnt/cdrom 3.修改本机上的YUM源配置文件 ...
- win下自动sftp脚本定时下载文件
缘起一个BA与客户交流的软件.但因为数据不能通过系统直连的方式进行获取. 对方只提供每天一份全量数据到指定文件夹下,我方自动通过sftp的方式去拉取. 一个只有简单几行的操作,想必肯定是不可能需写程序 ...