代码随想录算法训练营第46天|139.单词拆分、动态规划:多重背包、背包问题总结篇

139.单词拆分

https://leetcode.cn/problems/word-break/

class Solution {
public:
    bool wordBreak(string s, vector& wordDict) {
        unordered_set wordSet(wordDict.begin(), wordDict.end());
        vector 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();
    }
};

动态规划:多重背包

背包问题总结篇

 

你可能感兴趣的:(算法)