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. 任务二:1、选择器 2、连接集中状态的顺序 3、浮动的用发和原理 4、盒模型在IE和Google等不同浏览器的区别与联系

    1.选择器类型 标签选择器,类选择器,ID选择器,组合选择器,伪类和伪元素,属性选择器,子选择器,同胞选择器, :not()选择器 1.同胞选择器 相邻同胞标签: h2 + p{ color:red; ...

  2. 单个pdf提取测试

    # -*- coding: utf-8 -*- """ Created on Wed Feb 3 09:32:22 2016 pdf单个文件提取测试 @author: A ...

  3. cmake 编译 c++ dll 的一个例子(更新2:增加 python 调用方法)

    CMakeLists.txt project(xxx) add_library(xxx SHARED xxx.cpp) add_executable(yyy yyy.cpp) target_link_ ...

  4. linux设置和删除环境变量

    删除环境变量: unset -v PKG_CONFIG_PATH 添加环境变量: export PKG_CONFIG_PATH="你需要设置的路径", 例如: export LAN ...

  5. OBJ Loader Source Code

    https://github.com/ChrisJansson/ObjLoader http://www.codeproject.com/Articles/798054/SimpleScene-d-s ...

  6. UITextField限制输入文字

    一.viewDidLoad时监听通知 NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; [center addO ...

  7. PHP代码质量优化

    最近总结了一些平常写PHP代码时的一些优化分享给大家. 1.尽量使用绝对路径 相对路径中会检查很多路径,这时我们可以使用绝对路径,但绝对路径不利于后期维护,所以define定义的时候使用__FILE_ ...

  8. System.Diagnostics.Trace.Listeners

    System.Diagnostics.Trace.Listeners.Clear(); System.Diagnostics.Trace.AutoFlush = true; System.Diagno ...

  9. 在Razor中如何引入命名空间?("import namespace in razor view") 【转】

    原文链接 找了半天,原来如此: 在aspx中: <%@ Import Namespace = "Martian.Areas.SFC.Models" %><%@ I ...

  10. Python开发【第十八篇】:MySQL(二)

    视图 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需使用[名称]即可获取结果集,并可以将其当作表来使用. SELECT * FROM ( SEL ...