leetcode - 139. Word Break

动态规划问题
leetcode - 139. Word Break_第1张图片

class Solution {
public:
	bool wordBreak(string s, vector& wordDict) {
		int n = s.size(), ws = wordDict.size();
		unordered_set us(wordDict.begin(), wordDict.end());
		vector::iterator it;
		bool *dp = new bool[n + 1];
		fill(dp, dp + n + 1, 0);
		dp[0] = 1;
		for (int i = 1; i <= n; ++i)
		{
			for (it = wordDict.begin(); it != wordDict.end(); ++it)
			{
				if (i >= (*it).size() && dp[i - (*it).size()] && s.substr(i - (*it).size(), (*it).size()) == *it)
				{
					dp[i] = 1;
				}
			}
		}
		return dp[n];
	}
};

你可能感兴趣的:(leetcode,leetcode,动态规划)