题目:

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

  1. [
  2. ["aa","b"],
  3. ["a","a","b"]
  4. ] 

链接:  http://leetcode.com/problems/palindrome-partitioning/

题解:

一看到return all xxxx,就猜到可能要用回溯。这道题就是比较典型的递归+回溯。递归前要判断当前的子字符串是否palindrome,答案是false的话要continue。

Time Complexity - O(n*2n), Space Complexity - O(n*2n)

  1. public class Solution {
  2. public List<List<String>> partition(String s) {
  3. List<List<String>> res = new ArrayList<>();
  4. if(s == null || s.length() == 0)
  5. return res;
  6. ArrayList<String> list = new ArrayList<>();
  7. partition(res, list, s, 0);
  8. return res;
  9. }
  10.  
  11. private void partition(List<List<String>> res, ArrayList<String> list, String s, int pos) {
  12. if(pos == s.length()) {
  13. res.add(new ArrayList<String>(list));
  14. return;
  15. }
  16.  
  17. for(int i = pos + 1; i <= s.length(); i++) {
  18. String partition = s.substring(pos, i);
  19. if(!isPalindrome(partition))
  20. continue;
  21. list.add(partition);
  22. partition(res, list, s, i);
  23. list.remove(list.size() - 1);
  24. }
  25. }
  26.  
  27. private boolean isPalindrome(String s) {
  28. int lo = 0, hi = s.length() - 1;
  29.  
  30. while(lo < hi) {
  31. if(s.charAt(lo) != s.charAt(hi))
  32. return false;
  33. lo++;
  34. hi--;
  35. }
  36.  
  37. return true;
  38. }
  39. }

需要好好看看主方法来确定定量分析递归算法的时间复杂度。

二刷:

仔细想一想代码可以简化不少。主要分为三部分。1是题目给定的方法,2是辅助方法,用来递归和回溯,3是判断string是否是palindrome。注意考虑清楚需要多少变量,以及时间空间复杂度。

Time Complexity: O(n!)

Space Complexity: O(n ^ 2)

Java:

  1. public class Solution {
  2. public List<List<String>> partition(String s) {
  3. List<List<String>> res = new ArrayList<>();
  4. List<String> list = new ArrayList<>();
  5. partition(res, list, s);
  6. return res;
  7. }
  8.  
  9. private void partition(List<List<String>> res, List<String> list, String s) {
  10. if (s == null || s.length() == 0) {
  11. res.add(new ArrayList<String>(list));
  12. return;
  13. }
  14. for (int i = 0; i < s.length(); i++) {
  15. String subStr = s.substring(0, i + 1);
  16. if (isPalindrome(subStr)) {
  17. list.add(subStr);
  18. partition(res, list, s.substring(i + 1));
  19. list.remove(list.size() - 1);
  20. }
  21. }
  22. }
  23.  
  24. private boolean isPalindrome(String s) {
  25. if (s == null || s.length() < 2) {
  26. return true;
  27. }
  28. int lo = 0, hi = s.length() - 1;
  29. while (lo <= hi) {
  30. if (s.charAt(lo) != s.charAt(hi)) {
  31. return false;
  32. }
  33. lo++;
  34. hi--;
  35. }
  36. return true;
  37. }
  38. }

三刷:

依然是使用二刷的方法。

Java:

  1. public class Solution {
  2. public List<List<String>> partition(String s) {
  3. List<List<String>> res = new ArrayList<>();
  4. if (s == null || s.length() == 0) return res;
  5. partition(res, new ArrayList<>(), s);
  6. return res;
  7. }
  8.  
  9. private void partition(List<List<String>> res, List<String> list, String s) {
  10. if (s.length() == 0) {
  11. res.add(new ArrayList<String>(list));
  12. return;
  13. }
  14. for (int i = 0; i <= s.length(); i++) {
  15. String front = s.substring(0, i);
  16. if (isPalindrome(front)) {
  17. list.add(front);
  18. partition(res, list, s.substring(i));
  19. list.remove(list.size() - 1);
  20. }
  21. }
  22. }
  23.  
  24. private boolean isPalindrome(String s) {
  25. if (s == null || s.length() == 0) return false;
  26. int lo = 0, hi = s.length() - 1;
  27. while (lo < hi) {
  28. if (s.charAt(lo) != s.charAt(hi)) return false;
  29. lo++;
  30. hi--;
  31. }
  32. return true;
  33. }
  34. }

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. 对list集合中的对象进行排序(转载)

    原文链接:http://blog.csdn.net/veryisjava/article/details/51675036 Collections对List集合中的数据进行排序 有时候需要对集合中的元 ...

  2. 设计模式------STRATEGY(策略模式)

    http://blog.csdn.net/wuzhekai1985/article/details/6665197.仅供参考. 策略模式:实现替换功能,如cache替换算法:当发生Cache缺失时,C ...

  3. while循环语句

    while(循环条件,返回布尔类型)            {                代码执行的操作,或者打印输出. } do  whilw循环 do            {         ...

  4. JAVA调用WebService总结

    一.wximport自动生成代码 wsimport -keep -p com.test.client http://localhost:8080/test/services/TestService?w ...

  5. Xcode中 xx duplicate symbols for architecture i386错误提示

    今天在编译iOS项目时Xcode报如下错误: ld: 15 duplicate symbols for architecture i386 clang: error:linker command fa ...

  6. jq选取对象的方法

     $("#找id的")$(".找样式的")  $("div[id]") 选择所有含有id属性的div元素 $("input[nam ...

  7. OC3_字符串分割

    // // main.m // OC3_字符串分割 // // Created by zhangxueming on 15/6/11. // Copyright (c) 2015年 zhangxuem ...

  8. Putty + Vim + Color

    Putty + Vim + Color 参考: 1.Using colour schemes with vim and putty 2.Putty的颜色 3.Custom PuTTY Color Th ...

  9. 洛谷 P1108 低价购买

    P1108 低价购买 标签 动态规划 难度 提高+/省选- 题目描述 "低价购买"这条建议是在奶牛股票市场取得成功的一半规则.要想被认为是伟大的投资者,你必须遵循以下的问题建议:& ...

  10. How to Change Password Complexity Requirements in Windows XP

    Original Link: http://www.ehow.com/how_4812793_password-complexity-requirements-windows-xp.html#ixzz ...