给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,确定 s 是否可以被空格分割为一个或多个在字典里出现的单词。你可以假设字典中无重复的单词。
例如,给出
s = "leetcode",
dict = ["leet", "code"]。
返回 true 因为 "leetcode" 可以被切分成 "leet code"。

详见:https://leetcode.com/problems/word-break/description/

Java实现:

class Solution {
public boolean wordBreak(String s, List<String> wordDict) {
int n=s.length();
//dp[i]表示前i个字符能不能被dict完美划分
boolean[] dp=new boolean[n+1];
dp[0]=true;
for(int i=1;i<=n;++i){
for(int j=0;j<i;++j){
//substring是前闭后开
String tmp=s.substring(j,i);
if(dp[j]&&wordDict.contains(tmp)){
dp[i]=true;
break;
}
}
}
return dp[n];
}
}

参考:http://www.cnblogs.com/grandyang/p/4257740.html

139 Word Break 单词拆分的更多相关文章

  1. [LeetCode] 139. Word Break 单词拆分

    Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...

  2. LeetCode 139. Word Break单词拆分 (C++)

    题目: Given a non-empty string s and a dictionary wordDict containing a list of non-emptywords, determ ...

  3. [leetcode]139. Word Break单词能否拆分

    Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...

  4. Leetcode139. Word Break单词拆分

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

  5. leetcode 139. Word Break 、140. Word Break II

    139. Word Break 字符串能否通过划分成词典中的一个或多个单词. 使用动态规划,dp[i]表示当前以第i个位置(在字符串中实际上是i-1)结尾的字符串能否划分成词典中的单词. j表示的是以 ...

  6. 139. Word Break 以及 140.Word Break II

    139. Word Break Given a non-empty string s and a dictionary wordDict containing a list of non-empty  ...

  7. [LeetCode] Word Break II 拆分词句之二

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

  8. 139. Word Break

    题目: Given a string s and a dictionary of words dict, determine if s can be segmented into a space-se ...

  9. 【LeetCode】139. Word Break 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

随机推荐

  1. 通过反射调用一个单列的方法(单列必须有“getInstance”方法)

    Class<?> _clazz = Class.forName(_clazzName); if (_clazz != null) { Method _getInstance = _claz ...

  2. 区分虚拟机和machine simulator

    1 虚拟机和machine simulator的不同 虚拟机是让多个操作系统同时共用现有的硬件架构,它不会模拟新的硬件架构.qemu这样的模拟器是模拟新的硬件架构,这个架构和host不同.

  3. GitExtensions 官方手册(英文)

    在线版本(最新版):https://git-extensions-documentation.readthedocs.io/en/latest/ PDF版本(版本号 2.48):http://pan. ...

  4. 在C语言中使用libiconv进行编码转换的示例

    libiconv_sample.c #include <stdio.h> #include <malloc.h> #include "libiconv/iconv.h ...

  5. acd - 1427 - Nice Sequence(线段树)

    题意:一个由n个数组成的序列(序列元素的范围是[0, n]).求最长前缀 j .使得在这个前缀 j 中对于随意的数 i1 < i2.都满足随意的 m <= j.i1 在前 m 个数里出现的 ...

  6. css盒子模型详解一

    什么是CSS的盒子模式呢?为什么叫它是盒子?先说说我们在网页设计中常听的属性名:内容(content).填充(padding).边框(border).边界(margin), CSS盒子模式都具备这些属 ...

  7. Oracle:不同数据库版本导致的Ora-00918问题

    今天有同事反映,一个sql在10.0.2.4下面执行是好的,在11.0.2.3报Ora-00918问题. sql语句如下: SELECT kcdm, bjdm, f.kszc, f.jszc FROM ...

  8. 【扬中集训DAY1T1】 微信群

    [题目链接] 点击打开链接 [算法] 对问题稍加分析后,发现其实要求的就是 : C(N,K) + C(N,K+1) + C(N,K+2) + ... + C(N,N) 因为N最大10^9,K最大10^ ...

  9. 如何下载WDK

    随着Windows Vista和Windows Server 2008的相继发布,微软的驱动开发工具也进行了相应的更新换代.原来的驱动开发工具包叫做DDK(Driver Develpment Kit) ...

  10. Code-NFine:NFine介绍

    ylbtech-Code-NFine:NFine介绍 1. NFine平台介绍返回顶部 1. 使用时请务必保留来源,请勿用于违反我国法律的web平台.如诈骗等非法平台网站.版权最终解释权归<NF ...