139. Word Break
2016-11-06 09:43
218 查看
Given a string s and a dictionary of words dict, determine if
s can be segmented into a space-separated sequence of one or more dictionary words.
For example, given
s =
dict =
Return true because
分析:动态规划;
设置一个boolean数组,其中每个元素用来代表字符串s的前i个字符串,是不是能够用set中的字符串表示。
时间复杂度:O(N*N)
空间复杂度:O(N)
public class Solution {
public boolean wordBreak(String s, Set<String> wordDict) {
int n=s.length();
boolean[] flag=new boolean[n+1];
flag[0]=true;
for(int i=1;i<=n;i++){
for(int j=0;j<=i;j++){
String temp=s.substring(j,i);
if(flag[j]&&wordDict.contains(temp)){
flag[i]=true;
}
}
}
return flag
;
}
}
s can be segmented into a space-separated sequence of one or more dictionary words.
For example, given
s =
"leetcode",
dict =
["leet", "code"].
Return true because
"leetcode"can be segmented as
"leet code".
分析:动态规划;
设置一个boolean数组,其中每个元素用来代表字符串s的前i个字符串,是不是能够用set中的字符串表示。
时间复杂度:O(N*N)
空间复杂度:O(N)
public class Solution {
public boolean wordBreak(String s, Set<String> wordDict) {
int n=s.length();
boolean[] flag=new boolean[n+1];
flag[0]=true;
for(int i=1;i<=n;i++){
for(int j=0;j<=i;j++){
String temp=s.substring(j,i);
if(flag[j]&&wordDict.contains(temp)){
flag[i]=true;
}
}
}
return flag
;
}
}
相关文章推荐
- LeetCode(139) Word Break
- LeeCode_139 Word Break
- [LeetCode]题解(python):139-Word Break
- 139. Word Break(难)
- LeetCode139—Word Break
- Leetcode 139 Word Break
- Leetcode-139. Word Break
- LeetCode 139 Word Break
- leetcode 139. Word Break
- 139 Word Break
- LeetCode - 139/140 - Word Break
- 139. Word Break
- LeetCode(139)Word Break
- Leetcode#139 Word Break
- leetcode 139 Word Break(阿里测试岗笔试题)
- leetcode-139-Word Break
- LeetCode 139 Word Break (DFS 分词 解存在性判断)
- leetcode 139. Word Break
- 139. Word Break
- (M)Dynamic Programming:139. Word Break