leetcode139

class Solution {
public:
    bool wordBreak(string s, vector<string> wordDict)
    {
        vector<bool> wordB(s.length() + 1, false);
        wordB[0] = true;
        for (int i = 1; i < s.length() + 1; i++)
        {
            for (int j = i - 1; j >= 0; j--)
            {
                if (wordB[j] && find(wordDict.begin(), wordDict.end(), s.substr(j, i - j)) != wordDict.end())
                {
                    wordB[i] = true;
                    break;
                    //只要找到一种切分方式就说明长度为i的单词可以成功切分,
                    //因此可以跳出内层循环。            
                }
            }
        }
        return wordB[s.length()];
    }
};

猜你喜欢

转载自www.cnblogs.com/asenyang/p/9747354.html