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 = "aab",
Return
[
["aa","b"],
["a","a","b"]
]
链接: http://leetcode.com/problems/palindrome-partitioning/
题解:
一看到return all xxxx,就猜到可能要用回溯。这道题就是比较典型的递归+回溯。递归前要判断当前的子字符串是否palindrome,答案是false的话要continue。
Time Complexity - O(n*2n), Space Complexity - O(n*2n)
public class Solution {
public List<List<String>> partition(String s) {
List<List<String>> res = new ArrayList<>();
if(s == null || s.length() == 0)
return res;
ArrayList<String> list = new ArrayList<>();
partition(res, list, s, 0);
return res;
}
private void partition(List<List<String>> res, ArrayList<String> list, String s, int pos) {
if(pos == s.length()) {
res.add(new ArrayList<String>(list));
return;
}
for(int i = pos + 1; i <= s.length(); i++) {
String partition = s.substring(pos, i);
if(!isPalindrome(partition))
continue;
list.add(partition);
partition(res, list, s, i);
list.remove(list.size() - 1);
}
}
private boolean isPalindrome(String s) {
int lo = 0, hi = s.length() - 1;
while(lo < hi) {
if(s.charAt(lo) != s.charAt(hi))
return false;
lo++;
hi--;
}
return true;
}
}
需要好好看看主方法来确定定量分析递归算法的时间复杂度。
二刷:
仔细想一想代码可以简化不少。主要分为三部分。1是题目给定的方法,2是辅助方法,用来递归和回溯,3是判断string是否是palindrome。注意考虑清楚需要多少变量,以及时间空间复杂度。
Time Complexity: O(n!)
Space Complexity: O(n ^ 2)
Java:
public class Solution {
public List<List<String>> partition(String s) {
List<List<String>> res = new ArrayList<>();
List<String> list = new ArrayList<>();
partition(res, list, s);
return res;
}
private void partition(List<List<String>> res, List<String> list, String s) {
if (s == null || s.length() == 0) {
res.add(new ArrayList<String>(list));
return;
}
for (int i = 0; i < s.length(); i++) {
String subStr = s.substring(0, i + 1);
if (isPalindrome(subStr)) {
list.add(subStr);
partition(res, list, s.substring(i + 1));
list.remove(list.size() - 1);
}
}
}
private boolean isPalindrome(String s) {
if (s == null || s.length() < 2) {
return true;
}
int lo = 0, hi = s.length() - 1;
while (lo <= hi) {
if (s.charAt(lo) != s.charAt(hi)) {
return false;
}
lo++;
hi--;
}
return true;
}
}
三刷:
依然是使用二刷的方法。
Java:
public class Solution {
public List<List<String>> partition(String s) {
List<List<String>> res = new ArrayList<>();
if (s == null || s.length() == 0) return res;
partition(res, new ArrayList<>(), s);
return res;
}
private void partition(List<List<String>> res, List<String> list, String s) {
if (s.length() == 0) {
res.add(new ArrayList<String>(list));
return;
}
for (int i = 0; i <= s.length(); i++) {
String front = s.substring(0, i);
if (isPalindrome(front)) {
list.add(front);
partition(res, list, s.substring(i));
list.remove(list.size() - 1);
}
}
}
private boolean isPalindrome(String s) {
if (s == null || s.length() == 0) return false;
int lo = 0, hi = s.length() - 1;
while (lo < hi) {
if (s.charAt(lo) != s.charAt(hi)) return false;
lo++;
hi--;
}
return true;
}
}
Reference:
http://stackoverflow.com/questions/24591616/whats-the-time-complexity-of-this-algorithm-for-palindrome-partitioning
http://blog.csdn.net/metasearch/article/details/4428865
https://en.wikipedia.org/wiki/Master_theorem
http://www.cnblogs.com/zhuli19901106/p/3570430.html
https://leetcode.com/discuss/18984/java-backtracking-solution
https://leetcode.com/discuss/9623/my-java-dp-only-solution-without-recursion-o-n-2
https://leetcode.com/discuss/41626/concise-java-solution
https://leetcode.com/discuss/4788/shouldnt-we-use-dp-in-addition-to-dfs
131. Palindrome Partitioning的更多相关文章
- leetcode 131. Palindrome Partitioning 、132. Palindrome Partitioning II
131. Palindrome Partitioning substr使用的是坐标值,不使用.begin()..end()这种迭代器 使用dfs,类似于subsets的题,每次判断要不要加入这个数 s ...
- Leetcode 22. Generate Parentheses Restore IP Addresses (*) 131. Palindrome Partitioning
backtracking and invariant during generating the parathese righjt > left (open bracket and cloas ...
- Leetcode 131. Palindrome Partitioning
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- 78. Subsets(M) & 90. Subsets II(M) & 131. Palindrome Partitioning
78. Subsets Given a set of distinct integers, nums, return all possible subsets. Note: The solution ...
- [leetcode]131. Palindrome Partitioning字符串分割成回文子串
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- 【LeetCode】131. Palindrome Partitioning
Palindrome Partitioning Given a string s, partition s such that every substring of the partition is ...
- 131. Palindrome Partitioning (Back-Track, DP)
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- 131. Palindrome Partitioning(回文子串划分 深度优先)
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- Java for LeetCode 131 Palindrome Partitioning
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
随机推荐
- 查看源代码查找获取sd卡剩余容量的代码
下载android源码,找到app下的Settings应用源码,导入Settings项目(android项目源码) 查找“可用空间”得到 <string name="memory_av ...
- kettle Argument, Parameter, Variable
1. Argument, Parameter, Variable 的区别 a.Argument作为位置参数不能复用,而其他2个可以根据名称重复使用 b. Argument, Parameter作用域局 ...
- JAVA如何解析多层json数据
1. 使用标准的Json对象,如org.json.JSONObject json = new org.json.JSONObject(yourJsonString);然后通过get(keyString ...
- sublime text 使用
一.在sublime text中创建html.css.js文件 ctrl+shift+p(调出控制台) 然后输入 Set Syntax:html(也可以输入:ssh) Set Syntax:css ...
- 2016/7/6 神·CPU的人类极限在哪?
额,这其实是个搞怪贴 #include<stdio.h>int main(void){ int i,k; for(i=0;;i++) { k=i+222222222; printf(&qu ...
- 为UITextView添加与UITextField一样的边框——UITextField默认边框颜色、宽度、圆角
我的技术博客经常被流氓网站恶意爬取转载.请移步原文:http://www.cnblogs.com/hamhog/p/3789052.html,享受整齐的排版.有效的链接.正确的代码缩进.更好的阅读体验 ...
- linux svn authorization failed错误
authorization failed错误主要是conf/auth文件配置错误,可以参考如下配置: [aliases] # joe = /C=XZ/ST=Dessert/L=Snake City/O ...
- BrowserSync:跨浏览器实时同步预览
BrowserSync:跨浏览器实时同步预览 2016.09.11 官方网站:https://www.browsersync.io/ 项目仓库:https://github.com/Browsersy ...
- Sublime Text 3的快捷键
Sublime Text 3是一个非常了不起的软件,它不仅具有令人难以置信的内置功能(多行编辑和VIM模式),而且还支持插件.代码片段和其它许多东西. 今天,我们来总结一下Sublime Text 3 ...
- ASP.NET MVC验证 - jQuery异步验证
本文主要体验通过jQuery异步验证. 在很多的教材和案例中,MVC验证都是通过提交表单进行的.通过提交表单,可以很容易获得验证出错信息.因为,无论是客户端验证还是服务端验证,总能找到与Model属性 ...