代码随想录算法训练营第四十天|leetcode139题

一、leetcode第139题

本题是完全背包问题,由于可以重复使用,因此需要先遍历背包再遍历物品,dpi的含义是在长度为i处能否从数组中找到元素组成。

具体代码如下:

cpp 复制代码
class Solution {
public:
    bool wordBreak(string s, vector<string>& wordDict) {
    vector<bool>dp(s.length()+1,false);
    unordered_set<string>wordset(wordDict.begin(),wordDict.end());
    dp[0]=true;
    for(int i=1;i<=s.length();i++)
    {
        for(int j=0;j<i;j++)
        {
            string word=s.substr(j,i-j);
            if(wordset.find(word)!=wordset.end()&&dp[j]==true)
            {
                dp[i]=true;
            }
        }
    }
    return dp[s.length()];
    }
};
相关推荐
晴天的雨.99218 分钟前
[C++算法]快乐数
数据结构·c++·算法
孤狼warrior22 分钟前
SCTR 五次失败的安全 BN 路由器
人工智能·python·深度学习·算法·安全·yolo
影视飓风TIM24 分钟前
C++11 核心新特性完整梳理
数据结构·c++·算法
晴天的雨.99225 分钟前
[C++]算法双指针 复写0
数据结构·c++·算法
橘子汽水16839 分钟前
Leetcode 128,49最长连续序列,字母异位词分组
java·算法·leetcode
mmmmath_342 分钟前
LeetCode.438.找到字符串中所有字母异位词
数据结构·算法·leetcode
Nil2081 小时前
leetcode 22括号生成
算法·leetcode·深度优先
Navigator_Z1 小时前
LeetCode //C - 1237. Find Positive Integer Solution for a Given Equation
c语言·算法·leetcode