Palindrome Partitioning leetcode java
题目:
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"]
]
题解:
这道题还是一种找组合的可能性,类似于wordbreakii。
这里想法是,用递归循环找子问题的方法,把母串按所有组合可能性拆分,如果是回文,就加进来,当层数为s的length时就有一个结果了。
这里需要判断是否为回文。
利用validPalindrome的思想很容易就写出来了(这里不需要判断大小写还有有没有别的字符)。 代码如下:
1 public ArrayList<ArrayList<String>> partition(String s) {
2 ArrayList<String> item = new ArrayList<String>();
3 ArrayList<ArrayList<String>> res = new ArrayList<ArrayList<String>>();
4
5 if(s==null||s.length()==0)
6 return res;
7
8 dfs(s,0,item,res);
9 return res;
}
public void dfs(String s, int start, ArrayList<String> item, ArrayList<ArrayList<String>> res){
if (start == s.length()){
res.add(new ArrayList<String>(item));
return;
}
for (int i = start; i < s.length(); i++) {
String str = s.substring(start, i+1);
if (isPalindrome(str)) {
item.add(str);
dfs(s, i+1, item, res);
item.remove(item.size() - 1);
}
}
}
public boolean isPalindrome(String s){
int low = 0;
int high = s.length()-1;
while(low < high){
if(s.charAt(low) != s.charAt(high))
return false;
low++;
high--;
}
return true;
}
Palindrome Partitioning leetcode java的更多相关文章
- leetcode 132. Palindrome Partitioning II ----- java
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- Palindrome Partitioning——LeetCode
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- Palindrome Number leetcode java
题目: Determine whether an integer is a palindrome. Do this without extra space. click to show spoiler ...
- Java for LeetCode 132 Palindrome Partitioning II
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- Java for LeetCode 131 Palindrome Partitioning
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- [Leetcode][JAVA] Palindrome Partitioning II
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- Palindrome Partitioning II Leetcode java
题目: Given a string s, partition s such that every substring of the partition is a palindrome. Return ...
- [LeetCode] Palindrome Partitioning 拆分回文串
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- LeetCode: Palindrome Partitioning 解题报告
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
随机推荐
- 添加第一个控制器(Controllers)
在MVC体系架构中,输入请求是由控制器Controller来处理的(负责处理浏览器请求,并作出响应).在ASP.NET MVC中Controller本身是一个类(Class)通常继承于System.W ...
- BZOJ.3257.树的难题(树形DP)
题目链接 状态只与黑.白两点的颜色有关,于是用 \(f[x][i][j]\)表示当前以x为根节点,有\(i\)个黑点\(j\)个白点,使得x子树满足该条件的最小花费. 最后答案就是 \(min\{f[ ...
- 六省联考2017 Day2
目录 2018.3.27 Test 总结 T1 T2 T3 BZOJ.4873.[六省联考2017]寿司餐厅(最小割ISAP 最大权闭合子图) 考试代码 T1 T2 T3 2018.3.27 Test ...
- MVVM模式下关闭窗口的实现
通过行为来实现 实现界面与逻辑的分离 窗口关闭行为:其中含有布尔型的Close属性,将相应的关闭行为绑定到该属性上,则可以实现窗口的关闭行为,从而实现VM与View的分离 public class W ...
- 使用CefSharp在.Net程序中嵌入Chrome浏览器(四)——启动优化
在实际使用过程中,发现有的客户端会出现chrome加载网页过慢问题,定位后发现很多是因为设置系统代理所致,此时可以通过如下启动参数禁止系统代理. {"proxy-auto-detect&qu ...
- 【图像处理】基于OpenCV底层实现的直方图匹配
image processing 系列: [图像处理]图片旋转 [图像处理]高斯滤波.中值滤波.均值滤波 直方图匹配算法.又称直方图规定化.简单说.就是依据某函数.或者另外一张图片的引导,使得原图改变 ...
- Project 03- STM32F4xx PID controller
Project 03- STM32F4xx PID controller CMSIS files from ARM provides ARM Math functions. There are als ...
- [Go] 反射 - reflect.ValueOf()
类型 和 接口 由于反射是基于类型系统(type system)的,所以先简单了解一下类型系统. 首先 Golang 是一种静态类型的语言,在编译时每一个变量都有一个类型对应,例如:int, floa ...
- ARM 编程平台+coresight
http://www.keil.com/product/ DS-5:http://www.cnblogs.com/njseu/p/6023081.html http://www.arm.com/pro ...
- Dapper-translation 分布式监控系统
http://bigbully.github.io/Dapper-translation/ https://github.com/bigbully/Dapper-translation