题目:

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的更多相关文章

  1. leetcode 131. Palindrome Partitioning 、132. Palindrome Partitioning II

    131. Palindrome Partitioning substr使用的是坐标值,不使用.begin()..end()这种迭代器 使用dfs,类似于subsets的题,每次判断要不要加入这个数 s ...

  2. Leetcode 22. Generate Parentheses Restore IP Addresses (*) 131. Palindrome Partitioning

    backtracking and invariant during generating the parathese righjt > left  (open bracket and cloas ...

  3. Leetcode 131. Palindrome Partitioning

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

  4. 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 ...

  5. [leetcode]131. Palindrome Partitioning字符串分割成回文子串

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

  6. 【LeetCode】131. Palindrome Partitioning

    Palindrome Partitioning Given a string s, partition s such that every substring of the partition is ...

  7. 131. Palindrome Partitioning (Back-Track, DP)

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

  8. 131. Palindrome Partitioning(回文子串划分 深度优先)

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

  9. Java for LeetCode 131 Palindrome Partitioning

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

随机推荐

  1. Sublime Text2上搭建C/C++环境

    环境:Sublime Text2            win7 64位 1.下载Sublime Text2并安装     下载地址:http://www.sublimetext.com/ 2.需要用 ...

  2. C# lazy加载

    转自http://www.cnblogs.com/yunfeifei/p/3907726.html 在.NET4.0中,可以使用Lazy<T> 来实现对象的延迟初始化,从而优化系统的性能. ...

  3. 19_高级映射:一对多查询(使用resultMap)

    [需求] 查询订单以及订单明细的信息. 确定主查询表:订单表orders 确定关联查询表:订单明细表 orderdetail 在一对一查询的基础上添加订单明细表关联即可. [分析] 使用resultM ...

  4. Vijos P1325桐桐的糖果计划

    > P1325桐桐的糖果计划 标签:**图结构 强连通分量** 描述 桐桐很喜欢吃棒棒糖.他家处在一大堆糖果店的附近. 但是,他们家的区域经常出现塞车.塞人等情况,这导致他不得不等到塞的车或人走 ...

  5. C++重写与重载、重定义

    文章引用自:http://blog.163.com/clevertanglei900@126/blog/static/111352259201102441934870/ 重载overload:是函数名 ...

  6. What are the differences between small, minor, and major updates?

    Following contents are excerpted from the this website and only used for knowledge sharing:  Install ...

  7. mysql---多表关联

    首先要介绍一下集合的概念:集合具有无序性.唯一性. 无序性:指集合内部元素没有相对顺序的概念,对于两个集合而言,只要元素值和元素个数相同则两个集合相等. 唯一性:指集合内部元素不存在值相等的元素. 上 ...

  8. mysql---字符集详解

    常用的字符集包括ASCII ,GB2312 , GBK , UTF-8 ,Unicode 首先要知道 ASCII编码: 用一个字节来标识0-9的数字.大小写字母.及一些标点和不可见字符.1个字节8位, ...

  9. 怎么用程序获取远程url执行后的图片地址

    远程URL:https://121.199.16.229:8890/generate.cgi?rbid=1001&esn=22021434025005&pic=png&coun ...

  10. evaluateScript--evaluatePopoverScript--区别

    appcan.window.evaluateScript({})              //window.open()页面之间使用 appcan.window.evaluatePopoverScr ...