题目

https://leetcode.com/problems/word-break-ii/

题解

由 leetcode 139. Word Break | 139. 单词拆分(动态规划) 改造而来。

dp 过程示例:

class Solution {public List<String> wordBreak(String s, List<String> wordDict) {HashSet<String> dict = new HashSet<>();for (String word : wordDict) {dict.add(word);}int n = s.length();ArrayList<int[]>[][] dp = new ArrayList[n + 1][n + 1];for (int i = 1; i <= n; i++) {for (int j = i; j <= n; j++) {int L = j - i;int R = j;dp[L][R] = new ArrayList<>();if (dict.contains(s.substring(L, R))) {ArrayList<int[]> t = new ArrayList<>();t.add(new int[]{L, R});dp[L][R] = t;}for (int M = L + 1; M < R; M++) {// [L...M) + [M...R)if (dp[L][M] != null && dp[L][M].size() > 0 && dp[M][R] != null && dp[M][R].size() > 0) {dp[L][R].add(new int[]{L, M});dp[L][R].add(new int[]{M, R});}}}}Set<String> res = join(dp, 0, n, s);return new ArrayList<>(res);}public Set<String> join(ArrayList<int[]>[][] dp, int L, int R, String s) {ArrayList<int[]> pair = dp[L][R];Set<String> res = new HashSet<>();int i = 0;if (pair.size() % 2 == 1) {res.add(s.substring(L, R));i = 1;}for (; i + 1 < pair.size(); i += 2) {Set<String> lSet = join(dp, L, pair.get(i)[1], s);Set<String> rSet = join(dp, pair.get(i + 1)[0], R, s);for (String l : lSet) {for (String r : rSet) {res.add(l + " " + r);}}}return res;}
}

leetcode 140. Word Break II | 140. 单词拆分 II(动态规划)相关推荐

  1. 【DFS + 记忆化递归】LeetCode 140. Word Break II

    LeetCode 140. Word Break II Solution1:我的答案 纯DFS,在第31个case时超时,还是记录一下.. class Solution { // DFS public ...

  2. 【记忆化递归+DP】LeetCode 139. Word Break

    LeetCode 139. Word Break Solution1: 记忆化递归的典型套路题 参考网址:https://zxi.mytechroad.com/blog/leetcode/leetco ...

  3. LeetCode--139. 单词拆分(动态规划)

    单词拆分(动态规划) 1. 题目描述 2. 题目分析 3. C语言实现 4. Python实现 1. 题目描述 难度:中等 2. 题目分析 这道题的难点在于示例3,字符串包含字典中的所有单词,但是就是 ...

  4. LeetCode 140. 单词拆分 II

    文章目录 解法1:回溯 + 记忆数组,记录当前字符串 解法2:回溯 + 记忆数组,记录索引 `i` 到字符结尾的字符串能拆分的组合 https://leetcode-cn.com/problems/w ...

  5. LeetCode 140. 单词拆分 II(DP+回溯)

    1. 题目 给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,在字符串中增加空格来构建一个句子,使得句子中所有的单词都在词典中.返回所有这些可能的句子. 说明: 分隔时可以重复使 ...

  6. leetcode 140. 单词拆分 II(记忆化)

    给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,在字符串中增加空格来构建一个句子,使得句子中所有的单词都在词典中.返回所有这些可能的句子. 说明: 分隔时可以重复使用字典中的单 ...

  7. [leetcode] 140. 单词拆分 II

    class Solution {int n;vector<string>res;unordered_map<int,set<int>>hash;string s;v ...

  8. 140. Word Break II

    文章目录 1 题目理解 2 回溯+记忆化 1 题目理解 140与130的区别是,当字符串可分的时候,要求返回具体的分割字符串. 2 回溯+记忆化 对于字符串s,如果前面一部分是单词列表中的词,则拆分出 ...

  9. leetcode 139. Word Break | 139. 单词拆分(动态规划)

    题目 https://leetcode.com/problems/word-break/ 题解 时隔一天,再次遇到 dp 问题- 本题和 leetcode 375. Guess Number High ...

最新文章

  1. 扩增子图表解读6韦恩图:比较组间共有和特有OTU或分类单元
  2. java基础--集合Connection/Map
  3. webpack:全局变量、图片处理、样式兼容
  4. SpringCloud工作笔记049---nginx的安装及配置为简单的文件服务器
  5. ArrayList元素的排序 java 集合
  6. next数组_数据结构之数组与链表
  7. Cocos2d-x3.0-倾斜列表
  8. 《番茄工作法图解》一次只做一件事
  9. 网页与服务器数据库数据交互,网页与ACCESS数据库如何实现数据交互?
  10. Linux操作系统——桌面和终端基本操作【快捷键、一般模式、编辑模式、 命令模式】
  11. mysql导入人员信息_mysql中导入数据库
  12. 嵌入式Qt-做一个秒表
  13. 【基础算法】Gossip协议
  14. 关于云计算--openstack
  15. C#实例练习3:程序流程控制(2)
  16. 手机开机卡在android画面,手机一直停在开机画面怎么解决【图文】
  17. FlowFormer: Transformer结构光流估计
  18. final修饰类,修饰方法,修饰变量有什么特点?
  19. 实现shiro-remember功能
  20. 一个计算机高手的成长

热门文章

  1. 牛客 - Subset of Five(背包)
  2. CodeForces - 1303D Fill The Bag(贪心+模拟)
  3. UVA - 796 Critical Links(tarjan求割边)
  4. HDU - 1728 逃离迷宫(bfs)
  5. 机器学习-Stacking方法的原理及实现
  6. 动态规划算法-03背包问题
  7. 计算机文化基础知识点文件,计算机文化基础知识点.doc
  8. cocos2d-x游戏开发(二)开始菜单续
  9. C语言程序设计 | 模拟实现字符串操作函数:strlen, strcmp, strcpy, strcat, strchr, strstr
  10. C++内存管理全景指南