leetcode 139. Word Break
2017-10-19 12:02
190 查看
题目:
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
思路:
http://blog.csdn.net/gao1440156051/article/details/52192981
设dp[i]为前i个字符是否可以切割。
则dp[i]=dp[j]&&s.substr(j,i-j)
class Solution {
public:
bool wordBreak(string s, unordered_set<string>& wordDict) {
int len=s.size();
vector<bool> dp(len+1,false);
dp[0]=true;
for(int i=1;i<=len;i++){
for(int j=i-1;j>=0;j--){
if(dp[j]&&wordDict.count(s.substr(j,i-j))){
dp[i]=true;
break;
}
}
}
return dp[len];
}
};
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 =
"leetcode",
dict =
["leet", "code"].
Return true because
"leetcode"can be segmented as
"leet code".
思路:
http://blog.csdn.net/gao1440156051/article/details/52192981
设dp[i]为前i个字符是否可以切割。
则dp[i]=dp[j]&&s.substr(j,i-j)
class Solution {
public:
bool wordBreak(string s, unordered_set<string>& wordDict) {
int len=s.size();
vector<bool> dp(len+1,false);
dp[0]=true;
for(int i=1;i<=len;i++){
for(int j=i-1;j>=0;j--){
if(dp[j]&&wordDict.count(s.substr(j,i-j))){
dp[i]=true;
break;
}
}
}
return dp[len];
}
};
相关文章推荐
- LeetCode 139. Word Break
- [LeetCode]题解(python):139-Word Break
- Leetcode-139. Word Break
- 【LeetCode】139 - Word Break
- [leetcode]139. Word Break
- LeetCode - 139/140 - Word Break
- [LeetCode]--139. Word Break(Python)
- LeetCode139:Word Break
- LeetCode 139. Word Break
- LeetCode 139. Word Break
- leetcode[139] Word Break
- leetcode || 139、Word Break
- LeetCode 139 Word Break
- [LeetCode] 139 Word Break(BFS统计层数的方法)
- LeetCode139:Word Break
- LeetCode 139. Word Break
- leetcode 139 —— Word Break
- [leetcode]139. Word Break(Java)
- LeetCode 139. Word Break(单词分隔)
- leetcode 139. Word Break(dp,字典匹配)