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 ...
随机推荐
- LeetCode初体验—twoSum
今天注册了大名鼎鼎的LeetCode,做了一道最简单的算法题目: Given an array of integers, return indices of the two numbers such ...
- db2相关问题及解决方法
DB2相关问题及解决方法: 一.DB2中的代码页(codepage)问题. DB2备份时发生过代码页错误的问题,修改代码页后备份正常,但创建数据库时又发生代码页的错误.这是DB2服务器使用的代码页配置 ...
- 两天来学习C的感受
大学的时候曾经学习过C语言,教科书是谭浩强的绿色的书.当时根本没有好好学习,期末考试是靠老师画重点才过的. 那个时候稀里哗啦的完全听不明白,最揪心的是指针和文件操作(当时根本不知道这个世界上还有DB存 ...
- items 与iteritems
dict的items函数返回的是键值对的元组的列表,而iteritems使用的是键值对的generator. items当使用时会调用整个列表 iteritems当使用时只会调用值. >> ...
- Merge Into example
merge into users a using temp_users b on (a.userid = b.user_id) when matched then update set a.passw ...
- iOS开发——推送证书
(最近准备考试……空闲截图整理成博客)
- 【html】【19】高级篇--大事件时间轴
下载: http://sc.chinaz.com/jiaoben/131112181390.htm 其它: http://sc.chinaz.com/tag_jiaoben/shijianzhou.h ...
- Java编程思想读书笔记--第14章类型信息
7.动态代理 代理是基本的设计模式之一,它是你为了提供额外的或不同的操作,而插入的用来代替“实际”对象的对象.这些操作通常涉及与“实际”对象的通信,因此代理通常充当着中间人的角色. 什么是代理模式? ...
- 2016ACM竞赛训练暑期课期末考试 a题
描述 给出n个正整数,任取两个数分别作为分子和分母组成最简真分数,编程求共有几个这样的组合. 输入 第一行是一个正整数n(n<=600).第二行是n个不同的整数,相邻两个整数之间用单个空格隔开. ...
- MFC下拉框使用方法
Combo Box (组合框)控件很简单,可以节省空间.从用户角度来看,这个控件是由一个文本输入控件和一个下拉菜单组成的.用户可以从一个预先定义的列表里选择一个选项,同时也可以直接在文本框里面输入文本 ...