Leetcode no. 131
2016-05-30 00:47
405 查看
131. 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 =
Return
public class Solution {
public List<List<String>> partition(String s) {
List<List<String>> res= new LinkedList<>();
List<String> cur= new LinkedList<>();
searchPal(res, cur, s, 0);
return res;
}
private void searchPal(List<List<String>> res, List<String> cur, String s, int pos){
if (pos==s.length()) res.add(new LinkedList<>(cur));
else{
for (int i = pos; i < s.length(); i++) {
if (isPal(s, pos, i)){
cur.add(s.substring(pos,i+1));
searchPal(res, cur, s, i+1);
cur.remove(cur.size()-1);
}
}
}
}
private boolean isPal(String str, int l, int r){
if(l==r) return true;
while(l<r) if(str.charAt(l++)!=str.charAt(r--)) return false;
return true;
}
}
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"] ]
public class Solution {
public List<List<String>> partition(String s) {
List<List<String>> res= new LinkedList<>();
List<String> cur= new LinkedList<>();
searchPal(res, cur, s, 0);
return res;
}
private void searchPal(List<List<String>> res, List<String> cur, String s, int pos){
if (pos==s.length()) res.add(new LinkedList<>(cur));
else{
for (int i = pos; i < s.length(); i++) {
if (isPal(s, pos, i)){
cur.add(s.substring(pos,i+1));
searchPal(res, cur, s, i+1);
cur.remove(cur.size()-1);
}
}
}
}
private boolean isPal(String str, int l, int r){
if(l==r) return true;
while(l<r) if(str.charAt(l++)!=str.charAt(r--)) return false;
return true;
}
}
相关文章推荐
- 读书笔记(7) Android动画深入分析
- Unity基本操作一
- 读书笔记(8) 理解Window 和WindowManager
- geekband android #5 第十三次作业分析过程
- 正确在遍历中删除List元素
- 开发MapReduce程序 实验1
- 第六次作业
- mybatis
- 读书笔记(9) 四大组件的工作过程
- esri GDB API
- uva127--手风琴纸牌
- Android .9.png图片的制作与使用
- c++第六次实验
- 关于我的httpUrlConnection 出错的蠢事
- 8bit数据的指定位的置0或者置1操作
- JVM结构和细节
- 并行计算加速比
- python学习之路-4 内置函数和装饰器
- 一个demo理解什么是MVP
- 命名空间“UnityEngine”中不存在类型活命名空间名称“UI”怎么解决