140. 单词拆分 II

给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,在字符串中增加空格来构建一个句子,使得句子中所有的单词都在词典中。返回所有这些可能的句子。

说明:

分隔时可以重复使用字典中的单词。

你可以假设字典中没有重复的单词。

示例 1:

输入:

s = “catsanddog”

wordDict = [“cat”, “cats”, “and”, “sand”, “dog”]

输出:

[

“cats and dog”,

“cat sand dog”

]

示例 2:

输入:

s = “pineapplepenapple”

wordDict = [“apple”, “pen”, “applepen”, “pine”, “pineapple”]

输出:

[

“pine apple pen apple”,

“pineapple pen apple”,

“pine applepen apple”

]

解释: 注意你可以重复使用字典中的单词。

示例 3:

输入:

s = “catsandog”

wordDict = [“cats”, “dog”, “sand”, “and”, “cat”]

输出:

[]

class Solution {
private Map<String, List<String>> cache = new HashMap<>(); public List<String> wordBreak(String s, List<String> wordDict) {
return dfs(s, wordDict,0);
} private List<String> dfs(String s, List<String> wordDict, int offset){ if (offset == s.length()){
List<String> res = new ArrayList<>();
res.add("");
return res;
} if (cache.containsKey(s.substring(offset))){
return cache.get(s.substring(offset));
} List<String> res = new ArrayList<>();
for (String word : wordDict){
if (word.equals(s.substring(offset, Math.min(s.length(),offset + word.length())))){
List<String> next = dfs(s, wordDict, offset + word.length());
for (String str: next){
res.add((word + " "+ str).trim());
}
}
} cache.put(s.substring(offset),res);
return res;
}
}

Java实现 LeetCode 140 单词拆分 II(二)的更多相关文章

  1. Java实现 LeetCode 140 单词拆分II

    class Solution { public List<String> wordBreak(String s, List<String> wordDict) { List&l ...

  2. [LeetCode] 140. 单词拆分 II

    题目链接 : https://leetcode-cn.com/problems/word-break-ii/ 题目描述: 给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,在字符 ...

  3. Java实现 LeetCode 212 单词搜索 II(二)

    212. 单词搜索 II 给定一个二维网格 board 和一个字典中的单词列表 words,找出所有同时在二维网格和字典中出现的单词. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中&quo ...

  4. leetcode 140 单词拆分2 word break II

    单词拆分2,递归+dp, 需要使用递归,同时使用记忆化搜索保存下来结果,c++代码如下 class Solution { public: //定义一个子串和子串拆分(如果有的话)的映射 unorder ...

  5. Java实现 LeetCode 139 单词拆分

    139. 单词拆分 给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,判定 s 是否可以被空格拆分为一个或多个在字典中出现的单词. 说明: 拆分时可以重复使用字典中的单词. 你可 ...

  6. Java for LeetCode 140 Word Break II

    Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...

  7. 140. 单词拆分 II

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

  8. Java实现 LeetCode 212 单词搜索 II

    public class Find2 { public int[] dx={1,-1,0,0}; public int[] dy={0,0,1,-1}; class Trie{ Trie[] trie ...

  9. Java实现LeetCode 139 单词拆分

    public boolean wordBreak(String s, List<String> wordDict) { if(s.length() == 0){ return false; ...

随机推荐

  1. Git使用教程之SSH连接方式配置(二)

    什么是GitHub?这个网站就是提供Git仓库托管服务的. 什么是SSH Key?你的本地Git仓库和GitHub仓库之间的传输是通过SSH加密的,大白话理解就是这两个仓库如果要进行远程同步,则我们需 ...

  2. spring-boot+spring-cloud+maven-module 一个 maven多模块的微服务架构模版

    spring-boot-cloud-module spring-boot+spring-cloud+maven-module 一个 maven多模块的微服务架构模版. 新手上路的绝佳模版,只有必要的配 ...

  3. 03JAVA循环结构

    和JS\Python语句判断逻辑基本一致,不需要记录详细,只需要记录格式 一.for循环 for (初始化数据;判断语句:控制语句){ 循环体语句; } 二.while循环 初始化数据; while ...

  4. Struts2-Tiles 2.5.2 升级指南和通配符拓展

    最近工程从Struts2.3.18升级Struts2.5.2导致相关联的插件都需要升级到相同版本,其中tiles的变化最大. 1.web.xml上 listener org.apache.struts ...

  5. Docker & k8s 系列二:本机k8s环境搭建

    本篇将会讲解k8s是什么?本机k8s环境搭建,部署一个pod并演示几个kubectl命令,k8s dashboard安装. k8s是什么 k8s是kubernetes的简写,它是一个全新的基于容器技术 ...

  6. JSP+Servlet+JDBC+mysql实现的学生成绩管理系统

    项目简介 项目来源于:https://gitee.com/zzdoreen/SSMS 本系统基于JSP+Servlet+Mysql 一个基于JSP+Servlet+Jdbc的学生成绩管理系统.涉及技术 ...

  7. fastadmin后台:在表单操作项添加操作按钮并控制弹出页面的大小

    1.进入对应目录:eg(public/assets/js/backend/conmpany.js) 2.在field:'operate' 中添加buttons 源码: {field: 'operate ...

  8. Docker之docker log详解

    1.显示所有log docker logs [OPTIONS] <CONTAINER>   #显示某个容器的所有log docker-compose logs  #显示启动的所有容器的lo ...

  9. 记一条distinct 语句的优化。

    语句是这条 SELECT DISTINCT bank, account FROM sdb_payments WHERE status="succ": status 上有索引,但不是 ...

  10. 微信小程序通信录

    第一步:phone.wxml中 <view bindlongtap="clickPhone">{{phoneNum}}</view> 第二步:phone.j ...