139.单词拆分
https://leetcode.cn/problems/word-break/
class Solution {
public:
bool wordBreak(string s, vector<string>& wordDict) {
unordered_set<string> wordSet(wordDict.begin(), wordDict.end());
vector<bool> dp(s.size() + 1, false);
dp[0] = true;
for (int j = 1; j <= s.size(); j++) {
for (int i = j - 1; i >= 0; i--) {
string word(s.begin() + i, s.begin() + j);
if (wordSet.find(word) != wordSet.end() && dp[i] == true) dp[j] = true;
}
}
return dp.back();
}
};
动态规划:多重背包
背包问题总结篇