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

Commit3f39570

Browse files
MEDIUM/src/medium/WordBreak.java
1 parent82067f3 commit3f39570

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

‎MEDIUM/src/medium/WordBreak.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,28 @@
55
publicclassWordBreak {
66

77
//Jiuzhang gives a very good illustration for this problem!
8+
89
publicbooleanwordBreak(Strings,Set<String>wordDict) {
10+
intmaxLen =Integer.MIN_VALUE;
11+
for(Stringword :wordDict){
12+
maxLen = (word.length() >maxLen) ?word.length() :maxLen;
13+
}
14+
915
intn =s.length();
1016
boolean[]dp =newboolean[n+1];
1117
dp[0] =true;
1218
for(inti =1;i <=n;i++){
13-
for(intj =0;j <i;j++){
14-
if(!dp[j])continue;
19+
for(intj =1;j <=i &&j <=maxLen;j++){
20+
if(!dp[i-j])continue;
1521

16-
Stringsub =s.substring(j,i);
22+
Stringsub =s.substring(i-j,i);
1723
if(wordDict.contains(sub)){
1824
dp[i] =true;
1925
break;
2026
}
2127
}
2228
}
29+
2330
returndp[n];
2431
}
2532

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp