题目:

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. java--简单排序算法

    1.冒泡排序 排序原理: 过程简单,首先将第一个记录的关键字和第二个记录的关键字进行比较,若为逆序,则将两个记录交换,然后比较第二个记录与第三个记录得关键字.以此类推,直至第n-1个记录和第n个记录的 ...

  2. HW—字符串最后一个单词的长度,单词以空格隔开。

    描述 计算字符串最后一个单词的长度,单词以空格隔开. 知识点 字符串,循环 运行时间限制 0M 内存限制 0 输入 一行字符串,长度小于128. 输出 整数N,最后一个单词的长度. 样例输入 hell ...

  3. Hql 中实用查询时候 引号的使用

    出错代码://List vlist = this.getHibernateTemplate().find("from AndroidCustomer ct where ct.token = ...

  4. IO流详解之代码详解

    前面呢已经发了一些理解,整理了注释,整体来说IO这里难度不是很大,代码呢没有详细敲,只写了一个大概总结的内容如下: /** 一切皆文件:文件是所有操作系统保存数据和处理逻辑的唯一方式:不管是.exe, ...

  5. freemark页面中获取list循环中的counter

    如何在freemark页面中获取到当前list循环的counter 直接上代码 <#list lists as x> <#assign j=x?counter> ${j} // ...

  6. spring mvc 多视图配置

    <!-- jsp视图解析器--> <bean id="viewResolver" class="org.springframework.web.serv ...

  7. 给String添加reverse方法

    我们知道Array有个reverse方法,String则没有,但可以Array来实现,字符串有个split方法可以轻易的将String转换为Array. String.prototype.revers ...

  8. php二维数组,按照指定的key,去排序value值

    $arr = array( '11'=>array( 'a'=>1, 'b'=>2, ), '22'=>array( 'a'=>3, 'b'=>4, ), '33' ...

  9. C#基础(五)——类中私有构造函数作用

    如果类成员有private修饰符,就不允许在类范围以外访问这个类成员.对类构造函数应用private修饰符时,则禁止外部类创建该类的实例.尽管看上去有些不好理解(既然不能实例化,那么这个类还有什么用处 ...

  10. Markdown语法备忘

    标题 标题 标题是每篇文章都需要也是最常用的格式,在 Markdown 中,如果一段文字被定义为标题,只要在这段文字前加 # 号即可. # 一级标题 ## 二级标题 ### 三级标题 以此类推,总共六 ...