-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathT20_again.java
More file actions
58 lines (50 loc) · 1.41 KB
/
T20_again.java
File metadata and controls
58 lines (50 loc) · 1.41 KB
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
import java.util.ArrayList;
/**
* @Author:Aliyang
* @Data: Created in 下午3:00 18-7-13
* palindrome-partitioning:二刷
**/
public class T20_again {
ArrayList<ArrayList<String>> res=new ArrayList<>();
public ArrayList<ArrayList<String>> partition(String s) {
if (s.equals("")||s==null)
return res;
dfs(s,new ArrayList<>());
return res;
}
public void dfs(String s,ArrayList<String> cur){
if (s.equals("")){
res.add(new ArrayList<>(cur));
return;
}
for (int i=0;i<s.length();i++){
String str=s.substring(0,i+1);
if (isPalindrome(str)){
cur.add(str);
dfs(s.substring(i+1,s.length()),cur);
cur.remove(cur.size()-1);
}
}
}
private boolean isPalindrome(String s){
int start=0,end=s.length()-1;
while (start<end){
if (s.charAt(start)!=s.charAt(end))
return false;
start++;
end--;
}
return true;
}
public static void main(String[] args){
T20_again t=new T20_again();
String s="aab";
ArrayList<ArrayList<String>> res=t.partition(s);
for (ArrayList<String> cur:res){
for (String str:cur){
System.out.print(str+",");
}
System.out.println();
}
}
}