class Solution {
public List<String> wordBreak(String s, List<String> wordDict) { List<String> res = new ArrayList<>();
int max = 0, min = Integer.MAX_VALUE;
Set<String> set = new HashSet<>();
for (String word : wordDict) {
set.add(word);
max = Integer.max(max, word.length());
min = Integer.min(min, word.length());
} boolean f[] = new boolean[s.length() + 1];
f[0] = true;
for (int i = 1; i < s.length() + 1; i++) {
for (int j = Math.max(i - max, 0); j <= i - min; j++) {
if (f[j] && set.contains(s.substring(j, i))) {
f[i] = true;
break;
}
}
}
if (f[s.length()]) {
dfs(s, res, new StringBuilder(), set, 0, max, min);
}
return res;
} private void dfs(String s, List<String> res, StringBuilder sb, Set<String> set, int index, int max, int min) {
if (index == s.length()) {
sb.deleteCharAt(sb.length() - 1);
res.add(sb.toString());
return;
}
String str;
int size;
for (int i = index + min; i <= s.length() && i <= index + max; i++) {
if (set.contains(str = s.substring(index, i))) {
size = sb.length();
sb.append(str).append(' ');
dfs(s, res, sb, set, i, max, min);
sb.delete(size, sb.length());
}
}
}
}

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

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

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

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

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

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

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

  4. Java实现 LeetCode 139 单词拆分

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

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

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

  6. 140. 单词拆分 II

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

  7. 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 ...

  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. vue-multi-module【多模块集成的vue项目,多项目共用一份配置,可以互相依赖,也可以独立打包部署】

    基于 vue-cli 2 实现,vue 多模块.vue多项目集成工程 Github项目地址 : https://github.com/BothEyes1993/vue-multi-module 目标: ...

  2. MySQL安装(linux)

    Centos 安装mysql 安装mariadb yum install mariadb mariadb-server mariadb-devel 安装mysql rpm -qa | grep MyS ...

  3. transition完成事件

    当transition事件完成时调用函数(移动端导航的动画消失效果). <!doctype html> <html> <head> <meta charset ...

  4. node响应头缓存设置

    我把react项目分成4个板块,在路由的顶层 今天在手机上打开react项目的时候,发现平级路由跳转时某一个图片较多的板块图片总是渲染得很慢,这分明是重新发起请求了. 然后我先查一下react-rou ...

  5. React框架概述

    一.React框架概述 官网:https://reactjs.org/       最新版V16.10 中文网:https://zh-hans.reactjs.org/ 中文社区网:https://r ...

  6. DRF视图组件

    DRF视图组件: CVB模式继承----五层 from django.views import View # Django的View from rest_framework.views import ...

  7. 利用logrotate将mysql log截断

    https://blog.pythian.com/mysql-log-rotation/ 1.授权用户 CREATE USER 'log_rotate'@'localhost' IDENTIFIED ...

  8. Jenkins-插件开发-BUG-Messages类编译报错

    注意:下载Jenkins插件源码后报错Messages这个类怎么导包都报编译错误的问题! 今天从GitHub下载了Jenkins的一些插件源码,准备自己研究研究写个插件.但是发现每个源码中都存在一个编 ...

  9. 就为了一个原子操作,其他CPU核心罢工了

    i++问题 "阿Q赶快回去吧,隔壁二号车间的虎子说我们改了他们的数据,上门来闹事了" 由于老K的突然出现,我不得不提前结束与小黑的交流,赶回了CPU一号车间. 见到我回来,虎子立刻 ...

  10. B. Sleepy Game 博弈搜索

    题意:给一个有向图和起点,然后只有一名选手,这名选手可以随意挪动棋子,最终不能动的时候走过的边为奇数边为Win并输出路径,否则如果有环输出Draw,否则输出Lose; 题目链接 知道状态数最多只有n* ...