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"]
]

解题思路

求所有答案,首先排除动态规划,应该是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(回文子串划分 深度优先)的更多相关文章

  1. [LeetCode] 131. Palindrome Partitioning 回文分割

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

  2. Atcoder CODE FESTIVAL 2017 qual C D - Yet Another Palindrome Partitioning 回文串划分

    题目链接 题意 给定一个字符串(长度\(\leq 2e5\)),将其划分成尽量少的段,使得每段内重新排列后可以成为一个回文串. 题解 分析 每段内重新排列后是一个回文串\(\rightarrow\)该 ...

  3. 【HDU】4632 Palindrome subsequence(回文子串的个数)

    思路:设dp[i][j] 为i到j内回文子串的个数.先枚举所有字符串区间.再依据容斥原理. 那么状态转移方程为   dp[i][j] = dp[i][j-1] + dp[i+1][j] - dp[i+ ...

  4. Palindrome Partitioning (回文子串题)

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

  5. 【算法】最长回文子串 longest palindrome substring

    对于字符串S, 要找到它最长的回文子串,能想到的最暴力方法,应该是对于每个元素i-th都向左向右对称搜索,最后用一个数组span 记录下相对应元素i-th为中心的回文子串长度. 那么问题来了: 1. ...

  6. URAL 1297 Palindrome 最长回文子串

    POJ上的,ZOJ上的OJ的最长回文子串数据量太大,用后缀数组的方法非常吃力,所以只能挑个数据量小点的试下,真要做可能还是得用manacher.贴一下代码 两个小错,一个是没弄懂string类的sub ...

  7. Palindrome - POJ 3974 (最长回文子串,Manacher模板)

    题意:就是求一个串的最长回文子串....输出长度. 直接上代码吧,没什么好分析的了.   代码如下: ================================================= ...

  8. POJ 3974 Palindrome(最长回文子串)

    题目链接:http://poj.org/problem?id=3974 题意:求一给定字符串最长回文子串的长度 思路:直接套模板manacher算法 code: #include <cstdio ...

  9. Ural 1297 Palindrome 【最长回文子串】

    最长回文子串 相关资料: 1.暴力法 2.动态规划 3.中心扩展 4.Manacher法 http://blog.csdn.net/ywhorizen/article/details/6629268 ...

随机推荐

  1. react-native新导航组件react-navigation详解

    http://blog.csdn.net/sinat_17775997/article/details/70176688

  2. AWS系列-Amazon Simple Notification Service (SNS)

    SNS是一项 Web 服务,用于协调和管理向订阅终端节点或客户交付或发送消息的过程.在 Amazon SNS 中有两种类型的客户端:发布者和订阅者,也称为生产者和消费者.发布者通过创建消息并将消息发送 ...

  3. .NET基础知识(一、认识.Net)

    参考链接:http://blog.csdn.net/shanyongxu/article/details/50849111 认识.NET平台 可能很多人问什么是.NET框架,它包含了哪些内容?为开发程 ...

  4. WPF 渲染级别 (Tier)

    在WPF中,显卡的功能相差很大.当WPF评估显卡时,它会考虑许多因素,包括显卡上的RAM数量.对像素着色器(piexl shader)的支持(计算每个像素效果的内置程序,如透明效果),以及对顶点着色器 ...

  5. ArcGIS 空间数据库备份

    Expdp system/ddd@orcl directory=sdebak dumpfile=20170124_1_ServerBak.dmp schemas=(sde, ddd)

  6. ecstore 修改后台搜索框搜索字段的排序顺序

    Ecstore后台要添加搜索字段,只需要在dbschema里写filtertype和filterdefault就行了,但要修改搜索字段的顺序要怎么弄呢?? 经测试,直接在dbschema文件里修改字段 ...

  7. m2014-software->Word2010发布博客文章至Cnblogs

    转自:http://www.cnblogs.com/xfiver/archive/2011/12/26/2301591.html 昨夜喝醉,今天早起梳理情绪,看下如何使用word2010直接写cnbl ...

  8. java动态代理中的invoke方法是如何被自动调用的(转)

    一.动态代理与静态代理的区别. (1)Proxy类的代码被固定下来,不会因为业务的逐渐庞大而庞大: (2)可以实现AOP编程,这是静态代理无法实现的: (3)解耦,如果用在web业务下,可以实现数据层 ...

  9. JSON-Server 安装

    在后台还没给接口之前,使用JSON-Server搭建一台JSON服务器,将接口要返回的数据放在json文件里面.然后请求这些数据,这样我们可以先做一些东西,等后台接口好了之后直接替换就可以了,不必一直 ...

  10. AI画圆角矩形

    如何画圆角矩形:设置矩形圆角大小 第一种方法:点击圆角矩形在画布上点一下; [caption id="attachment_878" align="alignnone&q ...