forked from DengWangBao/Leetcode-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPalindromePartitioning.java
37 lines (33 loc) · 1.09 KB
/
PalindromePartitioning.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
public class PalindromePartitioning {
/**
* 容易错的地方
* f[0]的初始化
* f是开区间,flag是闭区间
* j > i-2
* i是开区间
*/
public List<List<String>> partition(String s) {
int n = s.length();
List<List<String>>[] f = new LinkedList[n + 1];
f[0] = new LinkedList<List<String>>();
f[0].add(Collections.EMPTY_LIST);
boolean[][] flag = new boolean[n][n];
for (int i = 0; i < n; i++) {
f[i + 1] = new LinkedList<List<String>>();
for (int j = 0; j <= i; j++) {
if (s.charAt(j) == s.charAt(i) && (j > i - 2 || flag[j + 1][i - 1])) {
flag[j][i] = true;
for (List<String> list : f[j]) {
List<String> list2 = new LinkedList<String>(list);
list2.add(s.substring(j, i + 1));
f[i + 1].add(list2);
}
}
}
}
return f[n];
}
}