-
Notifications
You must be signed in to change notification settings - Fork 1
/
131.分割回文串.java
62 lines (59 loc) · 1.52 KB
/
131.分割回文串.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
* @lc app=leetcode.cn id=131 lang=java
*
* [131] 分割回文串
*
* https://leetcode-cn.com/problems/palindrome-partitioning/description/
*
* algorithms
* Medium (63.24%)
* Likes: 163
* Dislikes: 0
* Total Accepted: 13.7K
* Total Submissions: 21.4K
* Testcase Example: '"aab"'
*
* 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串。
*
* 返回 s 所有可能的分割方案。
*
* 示例:
*
* 输入: "aab"
* 输出:
* [
* ["aa","b"],
* ["a","a","b"]
* ]
*
*/
class Solution {
public List<List<String>> partition(String s) {
List<List<String>> result = new ArrayList<>();
partition(result, new ArrayList<>(), s.toCharArray(), 0, s.length() - 1);
return result;
}
private void partition(List<List<String>> result, List<String> pre, char[] cs, int start, int end) {
if (start > end) {
result.add(pre);
return;
}
for (int i = start; i <= end; i++) {
if (isPalindrome(cs, start, i)) {
ArrayList<String> newPre = new ArrayList<>(pre);
newPre.add(new String(cs, start, i - start + 1));
partition(result, newPre, cs, i + 1, end);
}
}
}
private boolean isPalindrome(char[] cs, int start, int end) {
while (start < end) {
if (cs[start] != cs[end]) {
return false;
}
start++;
end--;
}
return true;
}
}