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

Commit4acb4f9

Browse files
authored
Create 139 Word Break
Java Word Break DP solution.
1 parentc781c49 commit4acb4f9

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

‎src/com/blankj/medium/139 Word Break

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
public boolean wordBreak(String s, List<String> wordDict) {
2+
HashSet<String> words=new HashSet(wordDict);
3+
int n=s.length();
4+
boolean[] dp=new boolean[n+1];
5+
6+
int len=0;
7+
for(String word: wordDict) len=Math.max(len,word.length());
8+
9+
dp[0]=true;
10+
for(int i=0;i<n;i++){
11+
if(!dp[i]) continue;
12+
13+
for(int l=1;i+l<=n && l <= len ; l++){
14+
if(words.contains(s.substring(i,i+l))) dp[i+1]=true;
15+
}
16+
}
17+
18+
return dp[n];
19+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp