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 ...
随机推荐
- hrbustoj 1142:围困(计算几何基础题,判断点是否在三角形内)
围困 Time Limit: 1000 MS Memory Limit: 65536 K Total Submit: 360(138 users) Total Accepted: 157(12 ...
- 运动目标检测ViBe算法
一.运动目标检测简介 视频中的运动目标检测这一块现在的方法实在是太多了.运动目标检测的算法依照目标与摄像机之间的关系可以分为静态背景下运动检测和动态背景下运动检测.先简单从视频中的背景类型来讨论. ...
- localStorage变更事件当前页响应新解-awen
html5的localStorage相信大家都是很熟悉了,但是在chrome等支持该对象的浏览器中(ie10除外),如果你监听storage变更事件你就会发现,当数据发生变化时本页是监听不到stora ...
- 使用webdriverwait封装查找元素方法
对于selenium原生的查找元素方法进行封装,在timeout规定时间内循环查找页面上有没有某个元素 这样封装的好处: 1.可以有效提高查找元素的效率,避免元素还没加载完就抛异常 2.相对于time ...
- 配置使用TargetFrameworks输出多版本类库
1.类库右键 2.修改配置 修改前: <Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <Targe ...
- Hibernate_day01--Hibernate配置文件详解_核心api
Hibernate映射配置文件(重点) 1 映射配置文件名称和位置没有固定要求 2 映射配置文件中,标签name属性值写实体类相关内容 (1)class标签name属性值实体类全路径 (2)id标签和 ...
- __del__()
__del__() 是类的内置函数,用于定义在脚本退出之前要执行的代码,因为有这个特性,通常被用来在脚本退出前关闭文件.关闭数据库连接.关闭网络连接等操作 [root@localhost ~]$ ca ...
- Excel 一个工作表进行按行数拆分
1. 如下Excel表,总共有120多行数据,如何将以50行数据为一个工作表进行拆分 Sub ZheFenSheet() Dim r, c, i, WJhangshu, WJshu, bt As Lo ...
- U盘插入拔出提示
Unit Unit1; Interface Uses Windows, Messages, SysUtils, Variants, classes, Graphics, Controls, Forms ...
- Java Swing 日历 控件
这是我改写的网上的DateChooser代码后的作品,使用效果如图所示.用法参考备注,以及Main函数中用法. /** * * Copyright: Ares. * All Rights Reserv ...