LeetCode: Palindrome Partitioning 解题报告
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:
https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/dfs/Partition_2014_1229.java
LeetCode: Palindrome Partitioning 解题报告的更多相关文章
- 【LeetCode】131. Palindrome Partitioning 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...
- 【LeetCode】Palindrome Partitioning 解题报告
[题目] Given a string s, partition s such that every substring of the partition is a palindrome. Retur ...
- [LeetCode] Palindrome Partitioning II 解题笔记
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- LeetCode:Palindrome Partitioning,Palindrome Partitioning II
LeetCode:Palindrome Partitioning 题目如下:(把一个字符串划分成几个回文子串,枚举所有可能的划分) Given a string s, partition s such ...
- LeetCode: Combination Sum 解题报告
Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...
- LeetCode: Palindrome Partitioning II 解题报告
Palindrome Partitioning II Given a string s, partition s such that every substring of the partition ...
- 【LeetCode】336. Palindrome Pairs 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 HashTable 相似题目 参考资料 日期 题目地 ...
- [leetcode]Palindrome Partitioning II @ Python
原题地址:https://oj.leetcode.com/problems/palindrome-partitioning-ii/ 题意: Given a string s, partition s ...
- [leetcode]Palindrome Partitioning @ Python
原题地址:https://oj.leetcode.com/problems/palindrome-partitioning/ 题意: Given a string s, partition s suc ...
随机推荐
- Js里面IF(var)表示什么意思?js中if的写法、含义
Q:if(一个变量),没有> < !=这些半短逻辑, 什么时候为真 什么时候为假? A:是判断逻辑,javascript是弱变量类型语言,通俗地就是没有数据类型.任意值都有逻辑值(真或假) ...
- ruby的json请求
require 'net/http'require 'uri'require 'json' customers = [{ "id" => 1123, " ...
- WIFEXITED WEXITSTATUS WIFSIGNALED(转)
wait的函数原型是: #include #include pid_t wait(int *status) 进 程一旦调用了wait,就立即阻塞自己,由wait自动分析是否当前进程的某个子进程已经退出 ...
- 简单几步让CentOS系统时间同步(转)
在使用CentOS系统的时候,我们可能会遇到时间不准的问题,那我们如何解决这个我问题呢,下面就来教大家一个CentOS系统时间同步的方法,希望大家可以解决自己所存在的疑问. CentOS系统时间同步的 ...
- Android--ListView 分割线
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout ...
- Digital Color Meter 颜色值提取工具
1.Digital Color Meter 简介 Digital Color Meter 是一款 Mac 自带的颜色值提取工具. 其它下载地址 Digital Color Meter for Mac, ...
- Oracle->mysql碰到的问题
1.大小写敏感的区别(如果服务器OS是linux). 在oracle中一般情况下不区分大小写.有时候我们在使用oracle不注意大小写的问题,表名和字段名不加双引号是不区分大小写的,像这样:in ...
- ASP.NET 的ClientIDMode属性
在ASP.NET 4.0之前我们总是要为控件的ClientID头疼,比如明明一个叫lblName的Label放在一个叫做grd的GridView里面后,在页面上改Label的ID就变成了诸如grd_c ...
- 使用 http 请求方式获取 eureka server的服务信息
对于一些系统不能接入 eureka server,又需要访问接入eureka server 的服务. 方法一:直接调用服务的地址是一种实现方式,弊端就是地址是写死的,万一服务地址变更则访问不到. 方法 ...
- Vue 动态组件、动画、插件
1 动态组件 ①简单来说: 就是几个组件放在一个挂载点下,然后根据父组件的某个变量来决定显示哪个,或者都不显示. ②动态切换: 在挂载点使用component标签,然后使用v-bind:is=”组件名 ...