leetcode 解题报告 Word Break
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的更多相关文章
- leetcode 解题报告 Word Ladder II
题目不多说了.见https://oj.leetcode.com/problems/word-ladder-ii/ 这一题我反复修改了两天半.尝试过各种思路,总是报TLE.终于知道这一题为什么是leet ...
- 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 ...
- LeetCode解题报告:Linked List Cycle && Linked List Cycle II
LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...
- leetcode解题报告(2):Remove Duplicates from Sorted ArrayII
描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...
- 【LeetCode】140. Word Break II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归求解 日期 题目地址:https://leetc ...
- 【LeetCode】139. Word Break 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- LeetCode 解题报告索引
最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中...... ...
- 【LeetCode OJ】Word Break II
Problem link: http://oj.leetcode.com/problems/word-break-ii/ This problem is some extension of the w ...
- LeetCode解题报告汇总! All in One!
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 把自己刷过的所有题目做一个整理,并且用简洁的语言概括了一下思路,汇总成了一个表格. 题目 ...
随机推荐
- 9.19 JS数组
数组:相同类型数据的集合强类型语言:1数组里面只能存放相同数据类型的数据 2定义数组的时候需要制定一个长度(可以存放的元素数量) 3内存空间连续集合:1.可以存放任意类型的数据 ...
- mysql锁
锁是计算机协调多个进程或线程并发访问某一资源的机制.在数据库中,除传统的计算资源(如CPU.RAM.I/O等)的争用以外,数据也是一种供许多用户共享的资源.如何保证数据并发访问的一致性.有效性是所有数 ...
- 【OpenCV】边缘检测:Sobel、拉普拉斯算子
推荐博文,博客.写得很好,给个赞. Reference Link : http://blog.csdn.net/xiaowei_cqu/article/details/7829481 一阶导数法:梯度 ...
- C++ Pointer-to-Member Selector
http://www.codeguru.com/cpp/cpp/article.php/c17401/C-Tutorial-PointertoMember-Function.htm https://m ...
- Which hashing algorithm is best for uniqueness and speed?
http://programmers.stackexchange.com/questions/49550/which-hashing-algorithm-is-best-for-uniqueness- ...
- ecshop后台模板设置中将非可编辑区改为可编辑区
原代码 <file name="category.dwt"> <region name="左边区域"> <lib>cart& ...
- MSSQL数据库索引的应用
一.索引的概念 索引就是加快检索表中数据的方法.数据库的索引类似于书籍的索引.在书籍中,索引允许用户不必翻阅完整个书就能迅速地找到所需要的信息.在数据库中,索引也允许数据库程序迅速地找到表中的数据,而 ...
- 微信小程序 教程及示例
作者:初雪链接:https://www.zhihu.com/question/50907897/answer/128494332来源:知乎著作权归作者所有,转载请联系作者获得授权.微信小程序正式公测, ...
- zabbix特性
在知道zabbix是什么之后,我们最关心的是zabbix有什么特性,了解特性之后,我们才能决定是否会使用zabbix,以及zabbix是否适合我们. 概述 Zabbix是一个高度集成的网络监控套件,通 ...
- hibernate4中取得connection的方法
在hibernate3中,使用了c3p0连接池,尝试了多种办法取得connection对象,以下两种可以使用. Java代码 Connection conn; // 方法1:hibernate4中将 ...