赞
踩
题目链接:139.单词拆分
文档链接:139.单词拆分
视频链接:动态规划之完全背包,你的背包如何装满?| LeetCode:139.单词拆分
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 i = 1; i<=s.size();i++){ for(int j = 0; j<i; j++){ string word = s.substr(j, i-j); if(wordSet.find(word) != wordSet.end() && dp[j]){ dp[i] = true; } } } return dp[s.size()]; } };
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。