Palindrome Partitioning——LeetCode
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之后再还原状态,首先确定解由两部分构成,每次只拆分后半部分,验证前半部分,如果前面是回文,则递归拆分并验证后半部分。
注意:DFS之后要还原状态,从上一个合法解之后继续遍历其他可能。
Talk is cheap>>
public class PalindromePartitioning {
public List<List<String>> partition(String s) {
List<List<String>> res = new ArrayList<>();
ArrayList<String> tmp = new ArrayList<>();
int length = s.length();
dfs(s, tmp, res, length);
return res;
}
public void dfs(String src, ArrayList<String> tmp, List<List<String>> res, int length) {
if (length == 0) {
res.add((ArrayList<String>) tmp.clone());
return;
}
for (int i = 1; i <= src.length(); i++) {
if (isValid(src.substring(0, i))) {
tmp.add(src.substring(0, i));
dfs(src.substring(i, src.length()), tmp, res, length - i);
tmp.remove(tmp.size() - 1);
}
}
}
public boolean isValid(String s) {
if (s == null || s.length() <= 1) {
return true;
}
int i = 0, j = s.length() - 1;
while (i < j) {
if (s.charAt(i) != s.charAt(j)) {
return false;
}
i++;
j--;
}
return true;
}
}
Palindrome Partitioning——LeetCode的更多相关文章
- Palindrome Partitioning leetcode java
题目: Given a string s, partition s such that every substring of the partition is a palindrome. Return ...
- [LeetCode] Palindrome Partitioning II 拆分回文串之二
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- [LeetCode] Palindrome Partitioning 拆分回文串
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- LeetCode:Palindrome Partitioning,Palindrome Partitioning II
LeetCode:Palindrome Partitioning 题目如下:(把一个字符串划分成几个回文子串,枚举所有可能的划分) Given a string s, partition s such ...
- leetcode@ [131/132] Palindrome Partitioning & Palindrome Partitioning II
https://leetcode.com/problems/palindrome-partitioning/ Given a string s, partition s such that every ...
- [LeetCode] Palindrome Partitioning II 解题笔记
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- [leetcode]Palindrome Partitioning II @ Python
原题地址:https://oj.leetcode.com/problems/palindrome-partitioning-ii/ 题意: Given a string s, partition s ...
- [leetcode]Palindrome Partitioning @ Python
原题地址:https://oj.leetcode.com/problems/palindrome-partitioning/ 题意: Given a string s, partition s suc ...
- LeetCode: Palindrome Partitioning 解题报告
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
随机推荐
- C++ 让 Win32 Console Application 程序后台运行
方法一:(无闪现) 添加 #pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRT ...
- Notice : Soft open files now is 1024, We recommend greater than 10000
在研究 workerman 时, 报了这个错误, 感觉只是个notice级别的, 就一直给忽略掉了, 今天有时间, 就查了一下. 其实本质就是 ulimit 这个命令 打开一个命令行, 输入 ulim ...
- css.day02
1.复合选择器 复合选择器 一般会有几个标签混合使用 .把多个组合成一个 称之为复合 1.1标签指定式 复合选择器 (交集选择器) 格式: 标记选择器+ 类名/ID名称 { 属性:值;} 实际情况用 ...
- 依赖注入与Unity(一) 介绍
在你学习依赖注入和Unity之前,你需要明白你为什么要使用它们.为了明白为什么要使用它们,你应该明白依赖注入和Unity能够帮助你解决什么类型的问题.作为介绍部分,这一章不会涉及太多关于Uni ...
- ASP.NET C#使用JavaScriptSerializer实现序列化与反序列化得到JSON
在JavaScriptSerializer中,我们可以看到下面可以使用的方法或者构造函数,它们都是实例方法: Member Description JavaScriptSerializer() 构造函 ...
- 使用Instant Client配置PL/SQL Developer
之前使用PL/SQL Developer都是直接在本机安装完整版的Oracle Database,一是省事,二是可以在本机做一些demo测试:最近换了台电脑,感觉Instant Client更简单一些 ...
- js 实现win7任务栏拖动效果
前言 在某个时刻, 我认识了一个朋友. 此人在我的教唆下, 踏上了js的不归路. 前天他问我, Win7任务栏拖动效果怎么实现. 我随口就跟他说, 这简单的一逼. 在我一晚上的折腾之后, 一份潦草的代 ...
- MFC框架类、文档类、视图类相互访问的方法
1.获取应用程序指针 CMyApp* pApp=(CMyApp*)AfxGetApp(); 2.获取主框架指针 CWinApp 中的公有成员变量 m_pMainWnd 就是主框架的指针 CMainFr ...
- CountDownLatch 和 CyclicBarrier 的运用及实现原理
I.CountDownLatch 和 CyclicBarrier 的运用 CountDownlatch: 定义: 其是一个线程同步的辅助工具,通过它可以做到使一条线程一直阻塞等待,直到其他线程完成其所 ...
- Asp.net MVC 与 Asp.net Web API 区别
Asp.Net Web API VS Asp.Net MVC 1.Asp.net MVC 是用来创建返回视图(Views)与数据的Web应用,而Asp.net Web API是一种简单轻松地成熟的HT ...