leetcode 139. 单词拆分

leetcode 139. 单词拆分

题目详情

题目链接
给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,判定 s 是否可以被空格拆分为一个或多个在字典中出现的单词。

  • 说明:
  1. 拆分时可以重复使用字典中的单词。
  2. 你可以假设字典中没有重复的单词。
  • 示例 1:
    输入: s = “leetcode”, wordDict = [“leet”, “code”]
    输出: true
    解释: 返回 true 因为 “leetcode” 可以被拆分成 “leet code”。
  • 示例 2:
    输入: s = “applepenapple”, wordDict = [“apple”, “pen”]
    输出: true
    解释: 返回 true 因为 “applepenapple” 可以被拆分成 “apple pen apple”。
    注意你可以重复使用字典中的单词。
  • 示例 3:
    输入: s = “catsandog”, wordDict = [“cats”, “dog”, “sand”, “and”, “cat”]
    输出: false

我的代码

class Solution {
private:
    int n;
    vector<bool> hasVisit;
public:
    bool canBreak(string s, vector<string> &wordDict, int loc) {
        if (s == "") {
            return true;
        }
        for (int i = 0; i < n; ++i) {
            if (s.substr(0, wordDict[i].size()) == wordDict[i]) {
                if (hasVisit[loc + wordDict[i].size()]) {
                    continue;
                }
                hasVisit[loc + wordDict[i].size()] = true;
                if (canBreak(s.substr(wordDict[i].size()), wordDict, loc + wordDict[i].size())) {
                    return true;
                }
            }
        }
        return false;
    }

    bool wordBreak(string s, vector<string>& wordDict) {
        set<char> sLetters(s.begin(), s.end()), wordLetters;
        for (auto word : wordDict) {
            wordLetters.insert(word.begin(), word.end());
        }
        if (wordLetters.size() < sLetters.size()) {
            return false;
        }
        for (auto &letter : sLetters) {
            if (!wordLetters.count(letter)) {
                return false;
            }
        }
        n = wordDict.size();
        hasVisit = vector<bool> (s.size() + 1, false);
        return canBreak(s, wordDict, 0);
    }
};

我的成绩

执行结果:通过
执行用时:4 ms, 在所有 C++ 提交中击败了96.81%的用户
内存消耗:7.8 MB, 在所有 C++ 提交中击败了100.00%的用户

一些想法

本道题我之前用的递归,超出了时间的限制。所以我之后进行了减枝优化,先判断wordDicet内是否包含了字符串s的全部字母,又设立访问标志,当某位ie已经访问过时,则不再继续访问。

执行用时为 0 ms 的范例

class Solution {
public: 
    int resEnd=0;
    bool wordBreak(string s, vector<string>& wordDict) {
        if(!s.size()||!wordDict.size())
        return false;
        vector<bool>dp(s.size()+1,false);
        dp[0]=true;
        for(int i=0;i<s.size();++i)
        {
            if(i==resEnd+1)
            return false;
            if(!dp[i])
            continue;
            for(auto& num:wordDict)
            {
                int newEND=i+num.size();
                if(newEND>s.size())
                continue;
                if(memcmp(&s[i],&num[0],num.size())==0)
                {
                    dp[newEND]=true;
                    resEnd=max(resEnd,newEND);
                }
            }
        }
        return dp.back();
        

    }
};

思考

范例用的动态规划。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值