Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit48c4f0e

Browse files
author
piggy1991
committed
partition
1 parent909c2ff commit48c4f0e

File tree

1 file changed

+149
-0
lines changed

1 file changed

+149
-0
lines changed

‎dfs/Partition_2014_1229.java

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
packageAlgorithms.dfs;
2+
3+
importjava.util.ArrayList;
4+
importjava.util.HashMap;
5+
importjava.util.List;
6+
7+
publicclassPartition_2014_1229 {
8+
// Solution 1: The DFS version.
9+
publicList<List<String>>partition1(Strings) {
10+
List<List<String>>ret =newArrayList<List<String>>();
11+
if (s ==null) {
12+
returnret;
13+
}
14+
15+
dfs(s,0,newArrayList<String>(),ret);
16+
returnret;
17+
}
18+
19+
publicstaticvoiddfs(Strings,intindex,List<String>path,List<List<String>>ret) {
20+
intlen =s.length();
21+
if (index ==len) {
22+
ret.add(newArrayList<String>(path));
23+
return;
24+
}
25+
26+
for (inti =index;i <len;i++) {
27+
Stringsub =s.substring(index,i +1);
28+
if (!isPalindrome(sub)) {
29+
continue;
30+
}
31+
32+
path.add(sub);
33+
dfs(s,i +1,path,ret);
34+
path.remove(path.size() -1);
35+
}
36+
}
37+
38+
publicstaticbooleanisPalindrome(Strings) {
39+
intlen =s.length();
40+
intleft =0;
41+
intright =len -1;
42+
43+
while (left <right) {
44+
if (s.charAt(left) !=s.charAt(right)) {
45+
returnfalse;
46+
}
47+
left++;
48+
right--;
49+
}
50+
51+
returntrue;
52+
}
53+
54+
// Solution 2: The DFS version with memory.
55+
publicList<List<String>>partition2(Strings) {
56+
List<List<String>>ret =newArrayList<List<String>>();
57+
if (s ==null) {
58+
returnret;
59+
}
60+
61+
// bug: new map error.
62+
dfs2(s,0,newArrayList<String>(),ret,newHashMap<String,Boolean>());
63+
returnret;
64+
}
65+
66+
publicstaticvoiddfs2(Strings,intindex,List<String>path,List<List<String>>ret,HashMap<String,Boolean>map) {
67+
intlen =s.length();
68+
if (index ==len) {
69+
ret.add(newArrayList<String>(path));
70+
return;
71+
}
72+
73+
for (inti =index;i <len;i++) {
74+
Stringsub =s.substring(index,i +1);
75+
if (!isPalindromeHash(sub,map)) {
76+
continue;
77+
}
78+
79+
path.add(sub);
80+
dfs2(s,i +1,path,ret,map);
81+
path.remove(path.size() -1);
82+
}
83+
}
84+
85+
// BUG 3: use boolean instead of Boolean.
86+
publicstaticbooleanisPalindromeHash(Strings,HashMap<String,Boolean>map) {
87+
intlen =s.length();
88+
intleft =0;
89+
intright =len -1;
90+
91+
if (map.get(s) !=null) {
92+
returnmap.get(s);
93+
}
94+
95+
map.put(s,true);
96+
while (left <right) {
97+
if (s.charAt(left) !=s.charAt(right)) {
98+
map.put(s,false);
99+
returnfalse;
100+
}
101+
left++;
102+
right--;
103+
}
104+
105+
returntrue;
106+
}
107+
108+
// Solution 3: Use DP to determine the palindrome first.
109+
publicList<List<String>>partition(Strings) {
110+
List<List<String>>ret =newArrayList<List<String>>();
111+
if (s ==null) {
112+
returnret;
113+
}
114+
115+
intlen =s.length();
116+
117+
// D[i][j]: if this a palindrom for s.substring(i, j + 1).
118+
boolean[][]D =newboolean[len][len];
119+
120+
for (intj =0;j <len;j++) {
121+
for (inti =0;i <=j;i++) {
122+
D[i][j] =s.charAt(i) ==s.charAt(j) && (j -i <=2 ||D[i +1][j -1]);
123+
}
124+
}
125+
126+
// bug: new map error.
127+
dfs3(s,0,newArrayList<String>(),ret,D);
128+
returnret;
129+
}
130+
131+
publicstaticvoiddfs3(Strings,intindex,List<String>path,List<List<String>>ret,boolean[][]D) {
132+
intlen =s.length();
133+
if (index ==len) {
134+
ret.add(newArrayList<String>(path));
135+
return;
136+
}
137+
138+
for (inti =index;i <len;i++) {
139+
Stringsub =s.substring(index,i +1);
140+
if (!D[index][i]) {
141+
continue;
142+
}
143+
144+
path.add(sub);
145+
dfs3(s,i +1,path,ret,D);
146+
path.remove(path.size() -1);
147+
}
148+
}
149+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp