[Leetcode] Palindrome Partitioning
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"]
]
Solution:
public class Solution {
public List<List<String>> partition(String s) {
List<List<String>> result=new ArrayList<List<String>>();
List<String> temp=new ArrayList<String>();
dfs(result,temp,s);
return result;
} private void dfs(List<List<String>> result, List<String> temp, String s) {
// TODO Auto-generated method stub
if(s.length()==0){
result.add(new ArrayList<String>(temp));
return;
}
for(int i=1;i<=s.length();++i){
String str=s.substring(0, i);
if(isPalindrome(str)){
temp.add(str);
dfs(result, temp, s.substring(i));
temp.remove(temp.size()-1);
}
}
} private boolean isPalindrome(String str) {
// TODO Auto-generated method stub
int end=str.length()-1;
int start=0;
while(start<end){
if(str.charAt(start)!=str.charAt(end))
return false;
start++;
end--;
}
return true;
}
}
[Leetcode] Palindrome Partitioning的更多相关文章
- LeetCode:Palindrome Partitioning,Palindrome Partitioning II
LeetCode:Palindrome Partitioning 题目如下:(把一个字符串划分成几个回文子串,枚举所有可能的划分) Given a string s, partition s such ...
- [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 拆分回文串之二
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 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 ...
- Leetcode: Palindrome Partitioning II
参考:http://www.cppblog.com/wicbnu/archive/2013/03/18/198565.html 我太喜欢用dfs和回溯法了,但是这些暴力的方法加上剪枝之后复杂度依然是很 ...
- LeetCode: Palindrome Partitioning [131]
[称号] Given a string s, partition s such that every substring of the partition is a palindrome. Retur ...
随机推荐
- 阿里云 SWAP
https://yq.aliyun.com/articles/52098 https://www.kejianet.cn/aliyun-swap/
- windows下Tomcat配置多实例
详情参见tomcat安装目录下RUNNING.txt中Advanced Configuration - Multiple Tomcat Instances部分. 问题源于下面这段tomcat官方文档的 ...
- 深入分析JavaWeb 技术内幕
1,通过浏览器请求一个资源,会发生以下几种过程 1) http的解析过程,包括对于http请求头和响应头中指令(控制用户浏览器的渲染行为和 服务器的执行逻辑)的解析 2)DNS的解析过程(根据域名获取 ...
- Zigzag
date: 2015-09-24 21:09:00 Print Zigzag 思路: 1 首先是按行输出,每行输出相同位置的下一间隔为row*2-2,行游标0 初始化时(每一行的首个斜向部分),j=i ...
- 关于SQLSERVER联合查询一点看法
首先看一段代码 这个数据库表我就不发了,这段代码的意思是:查询Book表中大于该类图书价格平均值的图书信息, 先看()里的内容,我一个表起了两个别名,让这个表的相同的id相等,查出平均分,然后再看() ...
- Windows硬件断点-实现单步异常
触犯单步异常 改变的是当前Eflags 而不是触发异常的Eflags 也就是 PUSHF MOV EAX, DWORD PTR[ESP] OR EAX, 0x100 MOV D ...
- 运行在linux上的mysql常用命令
mysql的注释:--或者# 1.mysql服务进程的命令 service mysqld start;#启动mysql服务 service mysqld status;#查看服务状态 service ...
- Java 中新增的 foreach 的用法
JDK1.5加入的增强for和循环. foreach语句使用总结 增强for(part1:part2){part3}; part2中是一个数组对象,或者是带有泛性的集合. part1定义了一个局部 ...
- JavaScript设计模式——方法的链式调用
方法的链式调用: (function() { //私有类 function _$ (els) { this.elements = []; for(var i = 0, len = els.length ...
- jquery尺寸:宽度与高度
width() 方法设置或返回元素的宽度(不包括内边距.边框或外边距). height() 方法设置或返回元素的高度(不包括内边距.边框或外边距). innerWidth() 方法返回元素的宽度(包括 ...