Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words.

For example, given
s = "leetcode",
dict = ["leet", "code"].

Return true because "leetcode" can be segmented as "leet code".

分析:

给出一个字符串s和一个词典,判断字符串s是否可以被空格切分成一个或多个出现在字典中的单词。

刚开始对这道题的理解有偏差,以为只要切出来的某一部分在字典里就可以了,后来发现不是这样,

这道题的意思是说,输入的字符串可以切成N段,每一段都必须在字典中。

举个例子:

s = "aaaaaaa",

dict = ["aaaa", "aa"],

返回false。

如果输入是

s = "aaab",

dict = ["a", "b"],

返回true。

初始化布尔型 数组, f[s.length() + 1],长度为s的长度加1, f[0,  1,  2, .......i - 1, i ] ,

f[i]表示的是字符串s从开始到第i个元素是否能用字典里的词表示。

遍历从开始到结尾的每个元素,先判断前半部分是否在字典里,再判断后面的是否在字典里。

public class Solution {
public boolean wordBreak(String s, Set<String> wordDict) {
if (s == null || s.length() == 0) {
return true;
}
int n = s.length(); boolean[] f = new boolean[n + 1];
//empty string is valid
f[0] = true; for(int i=0; i<s.length(); i++){
if(f[i]){
for(int j=i+1; j<=s.length(); j++){
String sub = s.substring(i, j);
if(wordDict.contains(sub)){
f[j]=true;
}
}
}
}
return f[n];
}
}

leetcode 解题报告 Word Break的更多相关文章

  1. leetcode 解题报告 Word Ladder II

    题目不多说了.见https://oj.leetcode.com/problems/word-ladder-ii/ 这一题我反复修改了两天半.尝试过各种思路,总是报TLE.终于知道这一题为什么是leet ...

  2. LeetCode解题报告—— Word Search & Subsets II & Decode Ways

    1. Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be con ...

  3. LeetCode解题报告:Linked List Cycle && Linked List Cycle II

    LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...

  4. leetcode解题报告(2):Remove Duplicates from Sorted ArrayII

    描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...

  5. 【LeetCode】140. Word Break II 解题报告(Python & C++)

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

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

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

  7. LeetCode 解题报告索引

    最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中......                        ...

  8. 【LeetCode OJ】Word Break II

    Problem link: http://oj.leetcode.com/problems/word-break-ii/ This problem is some extension of the w ...

  9. LeetCode解题报告汇总! All in One!

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 把自己刷过的所有题目做一个整理,并且用简洁的语言概括了一下思路,汇总成了一个表格. 题目 ...

随机推荐

  1. Instant Python 中文缩减版

    前言 本文主要来自<Python基础教程(第2版)>([挪]Magnus Lie Hetland著,司维 曾军崴 谭颖华译 人民邮电出版社) 中的“附录A 简明版本”,对于其中的有问题之处 ...

  2. mvn添加本地jar

    mvn install:install-file -DgroupId=com.oracle "-DartifactId=ojdbc6" "-Dversion=11.2.0 ...

  3. HTML学习笔记——选择器

    1> ID选择器.交叉选择器.群组选择器.子代选择器 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN& ...

  4. 只是为了好玩——Linux之父林纳斯自传

    http://yuedu.163.com/source/d227d1ce35d248b1a00471c11464d5d9_4

  5. Photoshop 融合属性 Unity Shader

    http://forum.unity3d.com/threads/free-photoshop-blends.121661/

  6. fixed的left:50%,漂浮

    .floor-box{width: 44px; border: 1px solid #ccc; position: %; z-index: } 漂浮距离,距中间50% .floor-box{width ...

  7. iOS7以上图片模糊效果

    模糊后的效果 模糊后的效果 框架UIImage+BlurredFrame里的 applyLightEffectAtFrame方法 例如 bgImage = [bgImage applyLightEff ...

  8. ServletContext

    1.为什么需要servletContext    需求1 需求2 --------------->解决之道servletContext     servletContext 1.ServletC ...

  9. Yii2 数据操作Query Builder(转)

    Query Builder $rows = (new \yii\db\Query()) ->select(['dyn_id', 'dyn_name']) ->from('zs_dynast ...

  10. linux下生成core dump文件方法及设置

    linux下生成core dump文件方法及设置    from:http://www.cppblog.com/kongque/archive/2011/03/07/141262.html core ...