leetcode Word Break

分析:题目意思是,给定词典的情况下,看看原字符串能不能全部成功地被给定的词典分割。

思路:

引入一个bool类型的数组match,match[i]标识长度为i的单词可不可以分词。最后返回match[len]就可以了。


class Solution {
public:
    //参考leetcode官网上的答案
    bool wordBreak(string s, unordered_set<string> &dict) {
        int len = s.length();
        if(len<1) return false;

        vector<bool> match(len,false); //match[i]标识长度为i的单词可不可以分词
        match[0] = true;

        for(int i=1; i<=len; i++){

            for(int j=i-1; j>=0; j--){
                if(match[j] && dict.find(s.substr(j,i-j))!=dict.end()){
                    match[i] = true;
                    break;      //只要找到一种切分方式就说明长度为i的单词可以成功切分,因此可以跳出内层循环。
                }
            }
        }
        return match[len];
    }
};



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值