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 ...
随机推荐
- 开启mysql日志及若干问题
今天学习了mysql日志功能,以前也有所了解,只不过没有深入的学习,所以趁着“余热”,把我从网上找到的资料与实践 结合起来,总结一下其基本用法.学习从来都不是无趣的,就看你怎么看待学习. 1.查看查询 ...
- Linux心得记录
2014.4.8 linux环境下如何删除一个目录? rm -r linux本身提供删除目录命令——rmdir,但是如果你要删除的目录中含有子目录或者子文件,那么该命令会提示“删除失败:目录非空“也就 ...
- hdu 1147:Pick-up sticks(基本题,判断两线段相交)
Pick-up sticks Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- 做一个WINDOWS下破解WIFI。不须要Linux抓包!
搬家了,没网了. 没有WIFI了! 想破解,只是没有Linux环境,不能抓包!破解! 于是自己动手开工. 在windows 下直接破解.貌似国内 还没看到.假设有了,那么请各位童鞋 提醒一下.赶急 要 ...
- 初始Dubbo
1. Dubbo是什么? Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案.简单的说,dubbo就是个服务框架,如果没有分布式的需求,其实是不需 ...
- 管理源代码的工具SVN与GIT
如何看待源代码 源代码是公司的重要资产 对应软件公司来说,源代码相当于固定资产>人才 所以源代码管理对于公司来说是最重要的事物之一 一.管理源代码的工具 SVN:集中式的源代码管理工具,通常必须 ...
- 基于注解的形式配置Bean
基于注解的方式配置Bean:也就说我们在每个Bean的类名前面注解一下,Spring会自动帮我们扫描Bean放进IOC容器中 I基于注解的方式配置Bean(没有依赖关系的Bean)有两个步骤: 1组件 ...
- 2017 Multi-University Training Contest - Team 6—HDU6098&&HDU6106&&HDU6103
HDU6098 Inversion 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6098 题目意思:题目很短,给出一个数组,下标从1开始,现在输出一个 ...
- 【Python算法】归纳、递归、归简
归简法(reduction) 指的是将某一问题转化成另一个问题,将一个未知问题归简成一个已解决的问题. 归纳法(induction) 首先要证明语句在某一基本情况下是成立的,然后证明他可以由一个对象推 ...
- (N)IO Frameworks in Java
(N)IO Frameworks in Java – Thread.currentThread.join() https://www.ashishpaliwal.com/blog/2008/10/ni ...