forked from damaohongtu/JavaInterview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordBreak_139.java
More file actions
38 lines (34 loc) · 999 Bytes
/
WordBreak_139.java
File metadata and controls
38 lines (34 loc) · 999 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package LeetCode;
import java.util.ArrayList;
import java.util.List;
/**
* @Classname WordBreak_139
* @Description 切分字符串,要刚刚好切分为字典中的单词
* 解法:
* @Date 19-5-10 下午11:01
* @Created by mao<[email protected]>
*/
public class WordBreak_139 {
public static boolean wordBreak(String s, List<String> wordDict) {
int len=s.length()+1;
boolean[] flags=new boolean[len];
flags[0]=true;
for(int i=1;i<len;i++){
for(int j=i-1;j>=0;j--){
if(flags[j]&&wordDict.contains(s.substring(j,i))){
flags[i]=true;
break;
}
}
}
return flags[len-1];
}
public static void main(String[] args){
List<String> wordDict=new ArrayList<>();
wordDict.add("leet");
wordDict.add("code");
String s="leetcode";
boolean result=wordBreak(s,wordDict);
System.out.print(result);
}
}