题目:

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. Hql 中实用查询时候 引号的使用

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

  2. asp:手机扫描二维码跳转手机版

    如果想手机扫描用pc版网站生成的二维码跳转到对应的手机版的话,请在pc端的首页的<head></head>标签里面加入下面内容:   <script src=" ...

  3. 常用的 Internet Browser adds-on/浏览器插件

    主要应用在Firefox, 或 Google Chrome 一.AdBlockPlus 广告屏蔽软件 二.GreaseMonkey 多样化网页 三.Dictionary.com 弹出单词的解释,来自 ...

  4. 模板:使用new delete 创建二维数组

    int **arr_matrix = new int*[n]; ; i < n; ++i) arr_matrix[i] = new int[n]; //内容 ; i < n; ++i) d ...

  5. Windows下Wamp装不上Memcache扩展

    windows下wamp装不上memcache扩展2015.03.20 No Comments 1,243 views用的是WAMP集成包,PHP版本5.5.12http://windows.php. ...

  6. linux中的sticky bit

    今天看到有个目录的权限是rwxrwxrwt 很惊讶这个t是什么,怎么不是x或者-呢?搜了下发现: 这个t代表是所谓的sticky bit. sticky bit: 该位可以理解为防删除位. 一个文件是 ...

  7. PHP5.3后在本机运行很慢的解决方法

    方法一:这是因为PHP 5.3在面对数据库配置信息中的“localhost”会犹豫,因此直接把这个地址改名为“127.0.0.1”,这个IP是IPv4下面的本地网络地址,实际作用和“localhost ...

  8. JQuery(三) Ajax相关

    JQuery大大简化了Ajax通用操作,开发者只需要指定请求URL,回调函数即可. 三个主要方法: $().param(obj):将obj参数(对象或数组)转化成查询字符串. {name:" ...

  9. Transaction的理解

    Transaction的理解   待完善......

  10. 【1】Bootstrap入门引言

    Bootstrap学习者要具备的一些要求: [1]xhtml常用标签的基础知识 [2]xhtml+css布局的基础知识 [3]html5+css3的基础知识 ===================== ...