leetcodeOJ 139. Word Break
2017-04-10 12:03
176 查看
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words,
determine if s can be segmented into a space-separated sequence of one or more dictionary words. You may assume the dictionary does not contain duplicate words.
For example, given
s =
dict =
Return true because
as
动态规划
代码如下:
determine if s can be segmented into a space-separated sequence of one or more dictionary words. You may assume the dictionary does not contain duplicate words.
For example, given
s =
"leetcode",
dict =
["leet", "code"].
Return true because
"leetcode"can be segmented
as
"leet code".
动态规划
代码如下:
class Solution { public: bool wordBreak(string s, vector<string>& wordDict) { if(wordDict.size() == 0) return false; int n = s.size(); vector<bool> dp(n+1, false); dp[0] = true; for(int i = 1; i <= n; i++){ for(int j = i-1; j >= 0; j--){ if(dp[j]){ string w = s.substr(j, i-j); vector<string>::const_iterator re = find(wordDict.begin(), wordDict.end(), w); if(re != wordDict.end()){ dp[i] = true; break; } } } } return dp ; } };
相关文章推荐
- Leetcode 139 Word Break
- LeeCode_139 Word Break
- leetcode 139. Word Break
- Leetcode 139, Word Break
- LeetCode 139. Word Break
- 【C++】【LeetCode】139. Word Break
- 139. Word Break
- leetcode 139. Word Break
- LeetCode139:Word Break
- [LeetCode]139.Word Break
- 【LeetCode从零单排】No198.House Robber &&No91.Decode Ways&&139 word break(动态规划典型应用)
- leetcode-139-Word Break
- leetcode——139——Word Break
- 【Medium】139. Word Break
- 139. Word Break
- 139. Word Break
- leetcode 139. Word Break
- LeetCode 139. Word Break (Medium)
- 139. Word Break
- leetcode[139]Word Break