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"]
  ]

Solution 0:

直接用DFS 做,实际上也是可以过Leetcode的检查的。

 public List<List<String>> partition(String s) {
List<List<String>> ret = new ArrayList<List<String>>();
if (s == null) {
return ret;
} dfs(s, 0, new ArrayList<String>(), ret);
return ret;
} public static void dfs(String s, int index, List<String> path, List<List<String>> ret) {
int len = s.length();
if (index == len) {
ret.add(new ArrayList<String>(path));
return;
} for (int i = index; i < len; i++) {
String sub = s.substring(index, i + 1);
if (!isPalindrome(sub)) {
continue;
} path.add(sub);
dfs(s, i + 1, path, ret);
path.remove(path.size() - 1);
}
} public static boolean isPalindrome(String s) {
int len = s.length();
int left = 0;
int right = len - 1; while (left < right) {
if (s.charAt(left) != s.charAt(right)) {
return false;
}
left++;
right--;
} return true;
}

Runtime: 520 ms

Solution 1:

用DFS 加上一个记忆。HashMap<String, Boolean> map 用它来记忆某一段是否回文,这样不用每一次都去判断回文。可以减少计算量。

 public List<List<String>> partition1(String s) {
List<List<String>> ret = new ArrayList<List<String>>();
List<String> path = new ArrayList<String>(); if (s == null) {
return ret;
} HashMap<String, Boolean> map = new HashMap<String, Boolean>(); dfs(s, path, ret, 0, map); return ret;
} public boolean isPalindrom(String s) {
int len = s.length();
if (len <= 1) {
return true;
} int left = 0;
int right = len - 1;
for (; left < right; left++, right--) {
if (s.charAt(right) != s.charAt(left)) {
return false;
}
} return true;
} /*
we use a map to store the solutions to reduce the times of computing.
*/
public void dfs(String s, List<String> path, List<List<String>> ret, int index, HashMap<String, Boolean> map) {
if (index == s.length()) {
ret.add(new ArrayList<String>(path));
return;
} for (int i = index; i < s.length(); i++) {
String sub = s.substring(index, i + 1); Boolean flag = map.get(sub);
if (flag == null) {
flag = isPalindrom(sub);
map.put(sub, flag);
} if (!flag) {
continue;
} path.add(sub);
dfs(s, path, ret, i + 1, map);
path.remove(path.size() - 1);
}
}

2014.12.29 Redo:

不过,最后的runtime没有什么大的改善。可能是数据量太小!

 // Solution 2: The DFS version with memory.
public List<List<String>> partition(String s) {
List<List<String>> ret = new ArrayList<List<String>>();
if (s == null) {
return ret;
} // bug: new map error.
dfs2(s, 0, new ArrayList<String>(), ret, new HashMap<String, Boolean>());
return ret;
} public static void dfs2(String s, int index, List<String> path, List<List<String>> ret, HashMap<String, Boolean> map) {
int len = s.length();
if (index == len) {
ret.add(new ArrayList<String>(path));
return;
} for (int i = index; i < len; i++) {
String sub = s.substring(index, i + 1);
if (!isPalindromeHash(sub, map)) {
continue;
} path.add(sub);
dfs2(s, i + 1, path, ret, map);
path.remove(path.size() - 1);
}
} // BUG 3: use boolean instead of Boolean.
public static boolean isPalindromeHash(String s, HashMap<String, Boolean> map) {
int len = s.length();
int left = 0;
int right = len - 1; if (map.get(s) != null) {
return map.get(s);
} map.put(s, true);
while (left < right) {
if (s.charAt(left) != s.charAt(right)) {
map.put(s, false);
return false;
}
left++;
right--;
} return true;
}

Runtime: 592 ms

Solution 2:

先用DP做一次判断是不是回文,然后再执行DFS,如果发现某条string不是回文,就可以直接退出,从而减少计算量。

 public List<List<String>> partition(String s) {
List<List<String>> ret = new ArrayList<List<String>>();
List<String> path = new ArrayList<String>(); if (s == null) {
return ret;
} boolean[][] isPalindrom = buildPalindromDP(s); dfs2(s, path, ret, 0, isPalindrom); return ret;
} /*
* Solution 2: Use DP to reduce the duplicate count.
* */
boolean[][] buildPalindromDP(String s) {
int len = s.length();
boolean[][] D = new boolean[len][len]; for (int j = 0; j < len; j++) {
for (int i = 0; i <= j; i++) {
if (j == 0) {
D[i][j] = true;
continue;
} D[i][j] = s.charAt(i) == s.charAt(j)
&& (j - i <= 2 || D[i + 1][j - 1]);
}
} return D;
} /*
we use a map to store the solutions to reduce the times of computing.
*/
public void dfs2(String s, List<String> path, List<List<String>> ret, int index, boolean[][] isPalindromDP) {
if (index == s.length()) {
ret.add(new ArrayList<String>(path));
return;
} for (int i = index; i < s.length(); i++) {
String sub = s.substring(index, i + 1);
if (!isPalindromDP[index][i]) {
continue;
} path.add(sub);
dfs2(s, path, ret, i + 1, isPalindromDP);
path.remove(path.size() - 1);
}
}

2014.12.29 Redo:

 // BUG 3: use boolean instead of Boolean.
public static boolean isPalindromeHash(String s, HashMap<String, Boolean> map) {
int len = s.length();
int left = 0;
int right = len - 1; if (map.get(s) != null) {
return map.get(s);
} map.put(s, true);
while (left < right) {
if (s.charAt(left) != s.charAt(right)) {
map.put(s, false);
return false;
}
left++;
right--;
} return true;
} // Solution 3: Use DP to determine the palindrome first.
public List<List<String>> partition(String s) {
List<List<String>> ret = new ArrayList<List<String>>();
if (s == null) {
return ret;
} int len = s.length(); // D[i][j]: if this a palindrom for s.substring(i, j + 1).
boolean[][] D = new boolean[len][len]; for (int j = 0; j < len; j++) {
for (int i = 0; i <= j; i++) {
D[i][j] = s.charAt(i) == s.charAt(j) && (j - i <= 2 || D[i + 1][j - 1]);
}
} // bug: new map error.
dfs3(s, 0, new ArrayList<String>(), ret, D);
return ret;
} public static void dfs3(String s, int index, List<String> path, List<List<String>> ret, boolean[][] D) {
int len = s.length();
if (index == len) {
ret.add(new ArrayList<String>(path));
return;
} for (int i = index; i < len; i++) {
String sub = s.substring(index, i + 1);
if (!D[index][i]) {
continue;
} path.add(sub);
dfs3(s, i + 1, path, ret, D);
path.remove(path.size() - 1);
}
}

Runtime: 524 ms, 实际上运行时间也没多少改善。可能是数据集大小的问题咯。

GitHub Link:

Partition.java

https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/dfs/Partition_2014_1229.java

LeetCode: Palindrome Partitioning 解题报告的更多相关文章

  1. 【LeetCode】131. Palindrome Partitioning 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...

  2. 【LeetCode】Palindrome Partitioning 解题报告

    [题目] Given a string s, partition s such that every substring of the partition is a palindrome. Retur ...

  3. [LeetCode] Palindrome Partitioning II 解题笔记

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

  4. LeetCode:Palindrome Partitioning,Palindrome Partitioning II

    LeetCode:Palindrome Partitioning 题目如下:(把一个字符串划分成几个回文子串,枚举所有可能的划分) Given a string s, partition s such ...

  5. LeetCode: Combination Sum 解题报告

    Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...

  6. LeetCode: Palindrome Partitioning II 解题报告

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

  7. 【LeetCode】336. Palindrome Pairs 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 HashTable 相似题目 参考资料 日期 题目地 ...

  8. [leetcode]Palindrome Partitioning II @ Python

    原题地址:https://oj.leetcode.com/problems/palindrome-partitioning-ii/ 题意: Given a string s, partition s  ...

  9. [leetcode]Palindrome Partitioning @ Python

    原题地址:https://oj.leetcode.com/problems/palindrome-partitioning/ 题意: Given a string s, partition s suc ...

随机推荐

  1. 解决flume运行中的一个异常问题!

    今天在本地测试flume的exec  监控文件   分割的问题!!!遇到各种141异常问题! 怀疑是在切割文件的时候超过了监控文本的时间,导致flume异常退出,,,所以增加了keep-alive 时 ...

  2. java线程池的应用

    package com.test; import java.util.concurrent.Callable; public class TestThread implements Callable& ...

  3. List<String> bikeList = Arrays.asList(bikeuuids);

    最近项目中  List<String> bikeList = Arrays.asList(bikeuuids);报错 而且console里面没有特别有用的 bikeList.add(&qu ...

  4. C#获取Web和非Web程序的目录

    几种方法如下:非Web程序 1.AppDomain.CurrentDomain.BaseDirectory 2.Environment.CurrentDirectory 3.HttpRuntime.B ...

  5. 使用Nexus搭建Maven服务器详细配置【转】

    为什么要搭建nexus私服,原因很简单,有些公司都不提供外网给项目组人员,因此就不能使用maven访问远程的仓库地址,所以很有必要在局域网里找一台有外网权限的机器,搭建nexus私服,然后开发人员连到 ...

  6. C#代码规范-编程秘笈

    原文地址:https://www.cnblogs.com/zzp0320/p/6949973.html 1.引言 本文是一套面向C# programmer和C# developer进行开发所应遵循的开 ...

  7. List元素排序简例

    前言:这种处理方式,在程序中偶尔会用的到,栗子很简单,关键是加强一下记忆,以及以备后用 1:实现Comparable接口的方式 1-1:没有使用泛型,重写compareTo()方法时需要判断类型及转换 ...

  8. .NET压缩图片保存 .NET CORE WebApi Post跨域提交 C# Debug和release判断用法 tofixed方法 四舍五入 (function($){})(jQuery); 使用VUE+iView+.Net Core上传图片

    .NET压缩图片保存   需求: 需要将用户后买的图片批量下载打包压缩,并且分不同的文件夹(因:购买了多个用户的图片情况) 文章中用到了一个第三方的类库,Nuget下载 SharpZipLib 目前用 ...

  9. python学习笔记——爬虫学习中的重要库urllib

    1 urllib概述 1.1 urllib库中的模块类型 urllib是python内置的http请求库 其提供了如下功能: (1)error 异常处理模块 (2)parse url解析模块 (3)r ...

  10. /proc 目录详细说明

    /proc路径详细: Linux 内核提供了一种通过 /proc 文件系统,在运行时访问内核内部数据结构.改变内核设置的机制.proc文件系统是一个伪文件系统,它只存在内存当中,而不占用外存空间.它以 ...