您的位置:首页 > 其它

leetcode139 Word Break

2017-08-22 10:35 337 查看
思路:

dp。

实现:

1 class Solution
2 {
3 public:
4     bool judge(vector<string>& wordDict, string s)
5     {
6         return find(wordDict.begin(), wordDict.end(), s) != wordDict.end();
7     }
8     bool wordBreak(string s, vector<string>& wordDict)
9     {
10         vector<bool> dp(s.length() + 1, 0);
11         dp[0] = true;
12         for (int i = 1; i <= (int)s.length(); i++)
13         {
14             for (int j = 0; j < i; j++)
15             {
16                 if (dp[j] && judge(wordDict, s.substr(j, i - j)))
17                 {
18                     dp[i] = true;
19                 }
20             }
21         }
22         return dp[s.length()];
23     }
24 };
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: