word-break 【LeetCode】

问题描述在这里插入图片描述

题目意思是:判断一个字符串能不能被分割成列表里的单词。

代码如下:

class Solution {
public:
    bool wordBreak(string s, vector<string>& wordDict) {

        unordered_set<string> dict(wordDict.begin(),wordDict.end());
        bool flag[s.size()+1] = {false};
        flag[0] = true;
        for(int i=1;i<=s.size();i++)
        {
            for(int j = i-1;j>=0;j--)
            {
                if(flag[j] && dict.find(s.substr(j,i-j))!=dict.end())
                {
                    flag[i]=true;
                    break;
                }
            }
        }
        return flag[s.size()];
    }
};

猜你喜欢

转载自blog.csdn.net/hhhhhh5863/article/details/89216054
今日推荐