Day46:动态规划 背包问题总结 LeedCode 139.单词拆分

139. 单词拆分

给你一个字符串 s 和一个字符串列表 wordDict 作为字典。如果可以利用字典中出现的一个或多个单词拼接出 s 则返回 true

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

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

提示:

  • 1 <= s.length <= 300
  • 1 <= wordDict.length <= 1000
  • 1 <= wordDict[i].length <= 20
  • swordDict[i] 仅由小写英文字母组成
  • wordDict 中的所有字符串 互不相同

思路:

**本题可转化为完全背包问题,**单词就是物品,字符串s就是背包,单词能否组成字符串s,就是问物品能不能把背包装满。

动态规划五部曲:

1.确定下标i与dp[j]的含义

dp[i];表示字符串长度为i时,是否可拆分为一个或多个在字典中出现的单词

2.确定递推公式

如果确定dp[j] 是true,且 [j, i) 这个区间的子串出现在字典里,那么dp[i]一定是true。(j < i )。

所以递推公式是 if(dp[j]&&[j, i) 这个区间的子串出现在字典里) dp[i]=true;

3.初始化

dp[0]就是递推的根基,dp[0]一定要为true,否则递推下去后面都都是false

下标非0的dp[i]初始化为false

4.确定遍历顺序

如果求组合数就是外层for循环遍历物品,内层for遍历背包

如果求排列数就是外层for遍历背包,内层for循环遍历物品

本题是求排列,不同顺序算不同情况

5.举例

代码参考:

java 复制代码
class Solution {
    public boolean wordBreak(String s, List<String> wordDict) {
        boolean[] dp=new boolean[s.length()+1];
        dp[0]=true;
        for(int i=1;i<dp.length;i++){
//遍历区间
            for(int j=0;j<i;j++){
                if(wordDict.contains(s.substring(j,i))&&dp[j]){
                    dp[i]=true;
                }
            }
        }
        return dp[s.length()];
    }
}
java 复制代码
class Solution {
    public boolean wordBreak(String s, List<String> wordDict) {
        boolean[] dp=new boolean[s.length()+1];
        dp[0]=true;
        for(int i=1;i<dp.length;i++){
//遍历字典中的单词
           for(String temp:wordDict){
            if(i>=temp.length()&&dp[i-temp.length()]&&temp.equals(s.substring(i-temp.length(),i))){
               dp[i]=true;
               break;
            }
           }
        }
        return dp[s.length()];
    }
}

回溯法:

java 复制代码
class Solution {
    public boolean wordBreak(String s, List<String> wordDict) {
   return backTracking(s,0,wordDict);
}
public boolean backTracking(String s,int stardIndex, List<String> wordDict){
    if(stardIndex>=s.length()){
        return true;
    }
    for(int i=stardIndex+1;i<=s.length();i++){
        String word=s.substring(stardIndex,i);
        if(wordDict.contains(word)&&backTracking(s,i,wordDict)){
         return true;
        }
    }
    return false;
}

}

上一种方法超时

回溯法优化:记忆化搜索,使用memory数组保存每次计算的以startIndex起始的计算结果,如果memory[startIndex]里已经被赋值了,直接用memory[startIndex]的结果。

java 复制代码
class Solution {
    public int[] memory;
    public boolean wordBreak(String s, List<String> wordDict) {
        memory=new int[s.length()+1];
   return backTracking(s,0,wordDict);
}
public boolean backTracking(String s,int stardIndex, List<String> wordDict){
    if(stardIndex>=s.length()){
        return true;
    }
    if(memory[stardIndex]==-1) return false;
    for(int i=stardIndex+1;i<=s.length();i++){
        String word=s.substring(stardIndex,i);
        if(wordDict.contains(word)&&backTracking(s,i,wordDict)){
         return true;
        }

    }
    memory[stardIndex]=-1;//以stardIndex为起始位置分割,拆分出来的单词无法匹配
    return false;
}

}

背包问题总结:

01背包:

二维dp数组01背包先遍历物品还是先遍历背包都是可以的,如果先遍历容量则第二层for循环是从小到大遍历

一维dp数组01背包只能先遍历物品再遍历背包容量,且第二层for循环是从大到小遍历。

完全背包

与01背包不同之处在于物品可无限取

内层循环一定是从小到大遍历的

纯完全背包,先遍历物品还是先遍历背包都是可以的

变形完全背包

求组合数(换零钱):先遍历物品

求排列数(组合总和IV):先遍历容量

Day44:动态规划 LeedCode 完全背包 518. 零钱兑换 II 377. 组合总和 Ⅳ-CSDN博客

求最小数:

为了防止覆盖,先将dp数组都设为Integer.MAX_VALUE

Day45:动态规划 爬楼梯(进阶版)LeedCode:322. 零钱兑换 279.完全平方数-CSDN博客

相关推荐
moonlifesudo24 分钟前
322:零钱兑换(三种方法)
算法
NAGNIP18 小时前
大模型框架性能优化策略:延迟、吞吐量与成本权衡
算法
美团技术团队19 小时前
LongCat-Flash:如何使用 SGLang 部署美团 Agentic 模型
人工智能·算法
Fanxt_Ja1 天前
【LeetCode】算法详解#15 ---环形链表II
数据结构·算法·leetcode·链表
侃侃_天下1 天前
最终的信号类
开发语言·c++·算法
茉莉玫瑰花茶1 天前
算法 --- 字符串
算法
博笙困了1 天前
AcWing学习——差分
c++·算法
NAGNIP1 天前
认识 Unsloth 框架:大模型高效微调的利器
算法
NAGNIP1 天前
大模型微调框架之LLaMA Factory
算法
echoarts1 天前
Rayon Rust中的数据并行库入门教程
开发语言·其他·算法·rust