给定一个非空字符串 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. Vue 之 npm 及 安装的包

    1  npm相关 1.1 npm 是 基于Node.js 的,所以要先安装Node.js 在浏览器地址栏输入https://nodejs.org/en/, 进入Node.js官网后,点击下载左边的稳定 ...

  2. Serializable 序列化 The byte stream created is platform independent. So, the object serialized on one platform can be deserialized on a different platform.

    Java 序列化接口Serializable详解 - 火星猿类 - 博客园 http://www.cnblogs.com/tomtiantao/p/6866083.html 深入理解JAVA序列化 - ...

  3. 如何使用Visual Studio构建libiconv

    参考博文:How to Build libiconv with Microsoft Visual Studio - CodeProject libiconv源码下载地址:libiconv - GNU ...

  4. hihoCoder 1582 Territorial Dispute 【凸包】(ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛)

    #1582 : Territorial Dispute 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 In 2333, the C++ Empire and the Ja ...

  5. html5--6-6 CSS选择器3

    html5--6-6 CSS选择器3 实例 学习要点 掌握常用的CSS选择器 了解不太常用的CSS选择器 什么是选择器 当我们定义一条样式时候,这条样式会作用于网页当中的某些元素,所谓选择器就是样式作 ...

  6. RandomUtils

    package com.cc.hkjc.util; import java.util.Random; public class RandomUtils {    /**     * 获取count个随 ...

  7. C/C++ 编译器优化

    0. gcc -o gcc -o 的优化仍然是机械的,想当然的.只有做到深入理解计算机系统,加深对编程语言的理解,才能写出最优化的代码. Linux下gcc 优化等级的介绍 gcc -o0 ⇒ 不提供 ...

  8. Android 获取View宽度

    /***************************************************************************** * Android 获取View宽度 * ...

  9. 【HNOI 2002】 营业额统计

    [题目链接] 点击打开链接 [算法] 观察式子 : 最小波动值 = min{|该天营业额 - 之前某天的营业额|} = min{该天营业额 - 该天营业额的前驱,该天营业额的后继 - 该天营业额} 用 ...

  10. Android适合组件化开发的路由框架:Launch

    1.概述 最近越来越不想写代码了,特别是一些重复性的代码,比如由于每次启动一个 Activity,我们都会很习惯的在 Activity 中写下: public static void launch(A ...