DFS与BFS超时的补救方法

题目:https://leetcode.cn/problems/word-break/description/?favorite=2cktkvj

139. 单词拆分

中等

给你一个字符串 s 和一个字符串列表 wordDict 作为字典。请你判断是否可以利用字典中出现的单词拼接出 s 。

注意:不要求字典中出现的单词全部都使用,并且字典中的单词可以重复使用。

示例 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

 参考:参考链接

KEY:单纯使用DFS发现大部分通过了,但是部分样例超时了

public class Solution {
    String s;
    List<String> wordDict;
    int n;
    boolean res = false;
    public boolean wordBreak(String s, List<String> wordDict) {
        this.s = s;
        this.wordDict = wordDict;
        n = s.length();
        for(int i=0;i<n;i++)
        {
            dfs(0,i);
        }
        return res;
    }
    void dfs(int start,int end)
    {
        //[start,end]期间是否可以作为一个单词
        String subString = s.substring(start,end+1);
        if(wordDict.contains(subString))
        {
            if(end == n-1)
            {
                //结束了
                res = true;
                return;
            }
            for(int i=end+1;i<n;i++)
                dfs(end+1,i);
        }
    }

    public static void main(String[] args) {
        Solution s = new Solution();
        System.out.println(s.wordBreak("leetcode", Arrays.asList("leet","code")));

    }
}

直接用记忆化搜索补救!

加入dp[][]//进行缓存

public class Solution {
    int[][] dp;
    String s;
    List<String> wordDict;
    int n;
    boolean res = false;
    public boolean wordBreak(String s, List<String> wordDict) {
        this.s = s;
        this.wordDict = wordDict;
        this.dp = new int[n+1][n+1];//1->true,0->null,-1->false
        Arrays.fill(dp,0);
        n = s.length();
        for(int i=0;i<n;i++)
        {
            dfs(0,i);
        }
        return res;
    }
    void dfs(int start,int end)
    {
        //[start,end]期间是否可以作为一个单词
        String subString = s.substring(start,end+1);
        if(wordDict.contains(subString))
        {
            dp[start][end] = 1;
            if(end == n-1)
            {
                //结束了
                res = true;
                return;
            }
            for(int i=end+1;i<n;i++)
            {
                if(dp[end+1,n-1] == 1)
                
                dfs(end+1,i);
            }
                
        }
        dp[start][end] =-1;
    }

    public static void main(String[] args) {
        Solution s = new Solution();
        System.out.println(s.wordBreak("leetcode", Arrays.asList("leet","code")));

    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Fars

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值