[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 ...
随机推荐
- CSS学习笔记----CSS3自定义字体图标
响应式网页字体图标 作者:大漠 日期:2014-01-28 点击:3220 @font-face Responsive 本文由大漠根据Jason的<Responsive Webfont Icon ...
- Python 小游戏 Bunny
最近在学习Python,所以上网找了一个小程序练练手. 关于这款名为[Bunny]的小游戏,详细请看下面的链接: http://www.oschina.net/translate/beginning- ...
- Delphi中怎么结束线程(这个线程是定时执行的)(方案二)
上篇博客中提出了一个问题:怎么结束一个定时循环执行的线程,并给出了一个解决方案,但是又出现了一个问题,详细去参考上一篇博客. 然后出去撒了个尿,突然脑子里出现了一个想法(看来工作和思考久了,出去走走, ...
- 【翻译十八】java-并发之锁对象
Lock Objects Synchronized code relies on a simple kind of reentrant lock. This kind of lock is easy ...
- poj 1004:Financial Management(水题,求平均数)
Financial Management Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 126087 Accepted: ...
- SSIS 包单元测试检查列表
1. 使用脚本任务(Script tasks) 组建的时候,在日志里增加一些调试信息,例如变量更新信息,可以帮助我们从日志中查看到变量是在何时何地更新的. 2. 使用ForceExecutionRes ...
- Linux环境下使用C/C++编写CGI(httpd)
step1下载: ftp://ftp.gnu.org/gnu/cgicc/ step2: tar xzf cgicc-X.X.X.tar.gz(用最新版本) cd cgicc-X.X.X ./conf ...
- 将数据导出成excel表
/// <summary> /// 生成excel表 /// </summary> /// <param name="dt">数据表</p ...
- 如何更快的删除String中的空格[未完]
背景:此文章主要源于网址[1]所描述的,文中大部分方法亦是[1]中实现的. 下面介绍集中删除空格的方法: 方法1:按空格分割后再拼接 /// <summary> /// 按空格分割后拼 ...
- SQLserver删除某数据库中所有表 方法 二
方便删除数据库中所有的数据表,清空数据库,有些有约束,不能直接delete,需要先删除库中的约束,代码如下: --删除所有约束DECLARE c1 cursor for select 'alter t ...