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

Commit2c5df57

Browse files
committed
word break
1 parenta1ffa1f commit2c5df57

File tree

2 files changed

+91
-12
lines changed

2 files changed

+91
-12
lines changed

‎dfs/Partition.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -88,16 +88,15 @@ boolean[][] buildPalindromDP(String s) {
8888
boolean[][]D =newboolean[len][len];
8989

9090
for (intj =0;j <len;j++) {
91-
for (inti =0;i <=j;i++) {
92-
if (j ==0) {
93-
D[i][j] =true;
94-
continue;
95-
}
96-
97-
// 注意,这里要加上j - i <= 2否则会越界
98-
D[i][j] =s.charAt(i) ==s.charAt(j)
99-
&& (j -i <=2 ||D[i +1][j -1]);
100-
}
91+
for (inti =0;i <=j;i++) {
92+
if (j ==0) {
93+
D[i][j] =true;
94+
continue;
95+
}
96+
97+
D[i][j] =s.charAt(i) ==s.charAt(j)
98+
&& (j -i <=2 ||D[i +1][j -1]);
99+
}
101100
}
102101

103102
returnD;

‎dp/WordBreak2.java

Lines changed: 82 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,21 @@ public static void main(String[] strs) {
3939
Algorithms.permutation.Stopwatchtimer2 =newAlgorithms.permutation.Stopwatch();
4040

4141
// HASH保存记忆
42-
List<String>list2 =wordBreak1(s,dict);
42+
wordBreak1(s,dict);
4343

4444
System.out
4545
.println("Computing time with ArrayDeque used as Queue/Deque: "
4646
+timer2.elapsedTime() +" millisec.");
47+
48+
Algorithms.permutation.Stopwatchtimer3 =newAlgorithms.permutation.Stopwatch();
49+
50+
// DFS+ 剪枝 3: 设置Flag 变量
51+
//http://fisherlei.blogspot.com/2013/11/leetcode-wordbreak-ii-solution.html
52+
wordBreak3(s,dict);
53+
54+
System.out
55+
.println("Computing time with ArrayDeque used as Queue/Deque: "
56+
+timer3.elapsedTime() +" millisec.");
4757

4858
//System.out.println(list.toString());
4959
}
@@ -126,7 +136,8 @@ public static List<String> wordBreak(String s, Set<String> dict) {
126136
}
127137

128138
// 我们用DFS模板来解决这个问题吧
129-
publicstaticvoiddfs2(Strings,Set<String>dict,List<String>path,List<String>ret,intindex) {
139+
publicstaticvoiddfs2(Strings,Set<String>dict,
140+
List<String>path,List<String>ret,intindex) {
130141
intlen =s.length();
131142
if (index ==len) {
132143
// 结束了。index到了末尾
@@ -195,4 +206,73 @@ public static boolean iswordBreak(String s, Set<String> dict) {
195206

196207
returnD[len];
197208
}
209+
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+
236+
// 我们用DFS模板来解决这个问题吧
237+
publicstaticvoiddfs3(Strings,Set<String>dict,
238+
List<String>path,List<String>ret,intindex,
239+
booleancanBreak[]) {
240+
intlen =s.length();
241+
if (index ==len) {
242+
// 结束了。index到了末尾
243+
StringBuildersb =newStringBuilder();
244+
for (Stringstr:path) {
245+
sb.append(str);
246+
sb.append(" ");
247+
}
248+
// remove the last " "
249+
sb.deleteCharAt(sb.length() -1);
250+
ret.add(sb.toString());
251+
return;
252+
}
253+
254+
// if can't break, we exit directly.
255+
if (!canBreak[index]) {
256+
return;
257+
}
258+
259+
booleanflag =false;
260+
for (inti =index;i <len;i++) {
261+
// 注意这些索引的取值。左字符串的长度从0到len
262+
Stringleft =s.substring(index,i +1);
263+
if (!dict.contains(left)) {
264+
// 如果左字符串不在字典中,不需要继续递归
265+
continue;
266+
}
267+
268+
// if can't find any solution, return false, other set it
269+
// to be true;
270+
flag =true;
271+
path.add(left);
272+
dfs3(s,dict,path,ret,i +1,canBreak);
273+
path.remove(path.size() -1);
274+
}
275+
276+
canBreak[index] =flag;
277+
}
198278
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp