码迷,mamicode.com
首页 > 其他好文 > 详细

139. Word Break

时间:2017-09-06 00:39:26      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:substr   size   vector   i++   bool   word   ict   result   solution   

 

 

class Solution {
public:
    bool wordBreak(string s, vector<string>& wordDict) {
        int length1 = wordDict.size();
        int maxlength = 0;
        for(int i = 0;i < length1;i++){
            if(wordDict[i].length() > maxlength)
                maxlength = wordDict[i].length();
        }
        int length2 = s.length();
        vector<bool> result(length2+1);
        result[0] = true;
        for(int i = 1;i <= length2;i++){
            result[i] = false;
            for(int j = 1;j <= maxlength && j <= i;j++){
                if(result[i-j] == false)
                    continue;
                else{
                    string str1 = s.substr(i-j,j);
                    for(int k = 0;k < length1;k++){
                        if(str1 == wordDict[k]){
                            result[i] = true;
                            break;
                        }
                    }
                }
            }
        }
        return result[length2];
    }
};

 

139. Word Break

标签:substr   size   vector   i++   bool   word   ict   result   solution   

原文地址:http://www.cnblogs.com/ymjyqsx/p/7482405.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!