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

Commit68c02a3

Browse files
committed
dp
1 parent2c5df57 commit68c02a3

File tree

1 file changed

+18
-42
lines changed

1 file changed

+18
-42
lines changed

‎dp/WordBreak2.java

Lines changed: 18 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -27,32 +27,32 @@ public static void main(String[] strs) {
2727

2828
System.out.println("Test");
2929

30-
Algorithms.permutation.Stopwatchtimer1 =newAlgorithms.permutation.Stopwatch();
31-
32-
// 递归模板,加剪枝
33-
List<String>list =wordBreak(s,dict);
34-
35-
System.out
36-
.println("Computing time with dfs and cut branch used as Queue/Deque: "
37-
+timer1.elapsedTime() +" millisec.");
38-
3930
Algorithms.permutation.Stopwatchtimer2 =newAlgorithms.permutation.Stopwatch();
4031

4132
// HASH保存记忆
4233
wordBreak1(s,dict);
4334

4435
System.out
45-
.println("Computing time withArrayDeque used as Queue/Deque: "
36+
.println("Computing time withDFS1: "
4637
+timer2.elapsedTime() +" millisec.");
4738

39+
Algorithms.permutation.Stopwatchtimer1 =newAlgorithms.permutation.Stopwatch();
40+
41+
// 递归模板,加剪枝
42+
wordBreak(s,dict);
43+
44+
System.out
45+
.println("Computing time with DFS2: "
46+
+timer1.elapsedTime() +" millisec.");
47+
4848
Algorithms.permutation.Stopwatchtimer3 =newAlgorithms.permutation.Stopwatch();
4949

5050
// DFS+ 剪枝 3: 设置Flag 变量
5151
//http://fisherlei.blogspot.com/2013/11/leetcode-wordbreak-ii-solution.html
5252
wordBreak3(s,dict);
5353

5454
System.out
55-
.println("Computing time withArrayDeque used as Queue/Deque: "
55+
.println("Computing time withDFS3: "
5656
+timer3.elapsedTime() +" millisec.");
5757

5858
//System.out.println(list.toString());
@@ -207,32 +207,6 @@ public static boolean iswordBreak(String s, Set<String> dict) {
207207
returnD[len];
208208
}
209209

210-
/*
211-
// 解法3:重新剪枝。
212-
*/
213-
214-
// 我们用DFS来解决这个问题吧
215-
publicstaticList<String>wordBreak3(Strings,Set<String>dict) {
216-
if (s ==null ||s.length() ==0 ||dict ==null) {
217-
returnnull;
218-
}
219-
220-
List<String>ret =newArrayList<String>();
221-
222-
// 记录切割过程中生成的字母
223-
List<String>path =newArrayList<String>();
224-
225-
intlen =s.length();
226-
booleancanBreak[] =newboolean[len];
227-
for (inti =0;i <len;i++) {
228-
canBreak[i] =true;
229-
}
230-
231-
dfs3(s,dict,path,ret,0,canBreak);
232-
233-
returnret;
234-
}
235-
236210
// 我们用DFS模板来解决这个问题吧
237211
publicstaticvoiddfs3(Strings,Set<String>dict,
238212
List<String>path,List<String>ret,intindex,
@@ -256,23 +230,25 @@ public static void dfs3(String s, Set<String> dict,
256230
return;
257231
}
258232

259-
booleanflag =false;
260233
for (inti =index;i <len;i++) {
261234
// 注意这些索引的取值。左字符串的长度从0到len
262235
Stringleft =s.substring(index,i +1);
263-
if (!dict.contains(left)) {
236+
if (!dict.contains(left) || !canBreak[i +1]) {
264237
// 如果左字符串不在字典中,不需要继续递归
265238
continue;
266239
}
267240

268241
// if can't find any solution, return false, other set it
269242
// to be true;
270-
flag =true;
271243
path.add(left);
244+
245+
intbeforeChange =ret.size();
272246
dfs3(s,dict,path,ret,i +1,canBreak);
247+
// 注意这些剪枝的代码. 关键在于此以减少复杂度
248+
if (ret.size() ==beforeChange) {
249+
canBreak[i +1] =false;
250+
}
273251
path.remove(path.size() -1);
274252
}
275-
276-
canBreak[index] =flag;
277253
}
278254
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp