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与dpj的含义

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

2.确定递推公式

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

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

3.初始化

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

下标非0的dpi初始化为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起始的计算结果,如果memorystartIndex里已经被赋值了,直接用memorystartIndex的结果。

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博客

相关推荐
Frostnova丶5 小时前
【算法笔记】数学知识
笔记·算法
吴可可1236 小时前
AutoCAD 2016与2014二次开发关键差异
算法
雨白7 小时前
哈希:以时间换空间的算法实战
算法
San813_LDD9 小时前
[数据结构]LeetCode学习
数据结构·算法·图论
x138702859579 小时前
c语言排雷游戏(基础版9*9)
c语言·算法·游戏
sheeta199810 小时前
LeetCode 每日一题笔记 日期:2026.06.06 题目:2196. 根据描述创建二叉树
笔记·算法·leetcode
小欣加油10 小时前
leetcode994 腐烂的橘子
数据结构·c++·算法·leetcode·bfs
QuZero11 小时前
Guava Cache Deep Dive
java·后端·算法·guava
随意起个昵称11 小时前
线性dp-LIS题目4(A Twisty Movement)
算法·动态规划