力扣139

/*

还是和决策树一样,从s的第0个位置开始遍历,

然后只要word是s的子串,那么则置为true,而且要注意边界条件

*/

class Solution {

public:

bool wordBreak(string s, vector<string>& wordDict) {

vector<bool> dp(s.length()+1,false);

dp[0]=true;

for(int i=0;i<s.length();i++){

for(auto word : wordDict){

if(dp[i]&&(i+word.length()<=s.length())&&word==s.substr(i,word.length())){

dp[i+word.length()]=dp[i];

}

}

}

return dp[s.length()];

}

};

相关推荐
黛色正浓4 小时前
leetCode-热题100-哈希合集(JavaScript)
javascript·leetcode·哈希算法
smj2302_796826524 小时前
解决leetcode第3777题使子字符串变交替的最少删除次数
python·算法·leetcode
Tisfy4 小时前
LeetCode 2110.股票平滑下跌阶段的数目:数学(一次遍历)
数学·算法·leetcode·题解
1024小神4 小时前
swift中 列表、字典、集合、元祖 常用的方法
数据结构·算法·swift
ULTRA??4 小时前
Informed RRT*实现椭圆启发式采样
c++·算法
Swizard4 小时前
告别样本不平衡噩梦:Focal Loss 让你的模型学会“划重点”
算法·ai·训练
程序员麻辣烫5 小时前
傲慢与偏见
职场和发展
亭台5 小时前
【Matlab笔记_23】MATLAB的工具包m_map的m_image和m_pcolor区别
笔记·算法·matlab
李玮豪Jimmy5 小时前
Day39:动态规划part12(115.不同的子序列、583.两个字符串的删除操作、72.编辑距离)
算法·动态规划