Word Break leetcode java
题目:
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".
题解:
这道题的题解转载自Code ganker,他写的很好。地址:http://blog.csdn.net/linhuanmars/article/details/22358863
“原题链接: http://oj.leetcode.com/problems/word-break/
这道题仍然是动态规划的题目,我们总结一下动态规划题目的基本思路。首先我们要决定要存储什么历史信息以及用什么数据结构来存储信息。然后是最重要的递推式,就是如从存储的历史信息中得到当前步的结果。最后我们需要考虑的就是起始条件的值。
接
下来我们套用上面的思路来解这道题。首先我们要存储的历史信息res[i]是表示到字符串s的第i个元素为止能不能用字典中的词来表示,我们需要一个长度
为n的布尔数组来存储信息。然后假设我们现在拥有res[0,...,i-1]的结果,我们来获得res[i]的表达式。思路是对于每个以i为结尾的子
串,看看他是不是在字典里面以及他之前的元素对应的res[j]是不是true,如果都成立,那么res[i]为true,写成式子是
假
设总共有n个字符串,并且字典是用HashSet来维护,那么总共需要n次迭代,每次迭代需要一个取子串的O(i)操作,然后检测i个子串,而检测是
constant操作。所以总的时间复杂度是O(n^2)(i的累加仍然是n^2量级),而空间复杂度则是字符串的数量,即O(n)。代码如下:
”
1 public boolean wordBreak(String s, Set<String> dict) {
2 if(s==null || s.length()==0)
3 return true;
4 boolean[] res = new boolean[s.length()+1];
5 res[0] = true;
6 for(int i=0;i<s.length();i++)
7 {
8 StringBuilder str = new StringBuilder(s.substring(0,i+1));
9 for(int j=0;j<=i;j++)
{
if(res[j] && dict.contains(str.toString()))
{
res[i+1] = true;
break;
}
str.deleteCharAt(0);
}
}
return res[s.length()];
}
Word Break leetcode java的更多相关文章
- leetcode 140. Word Break II ----- java
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...
- Word Break - LeetCode
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- Word Ladder leetcode java
题目: Given two words (start and end), and a dictionary, find the length of shortest transformation se ...
- Word Search leetcode java
题目: Given a 2D board and a word, find if the word exists in the grid. The word can be constructed fr ...
- LeetCode之“动态规划”:Word Break && Word Break II
1. Word Break 题目链接 题目要求: Given a string s and a dictionary of words dict, determine if s can be seg ...
- Word Break II leetcode java
题目: Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where e ...
- LeetCode Word Break II
原题链接在这里:https://leetcode.com/problems/word-break-ii/ 题目: Given a string s and a dictionary of words ...
- [LeetCode] 139. Word Break 单词拆分
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...
- [LeetCode] 140. Word Break II 单词拆分II
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add space ...
随机推荐
- PHP 笔记——文件引用
1. 文件路径 "文件路径"指的是被包含文件所在的绝对路径或相对路径. 在相对路径中,"./"表示当前目录,"../"表示当前目录的上级目录 ...
- [BZOJ4571][SCOI2016]美味(贪心+主席树)
经典问题,按位贪心,每次需要知道的是”在这一位之前的位都以确定的情况下,能否找到这一位是0/1的数”,这就是在询问[L,R]内某个值域区间是否有数,主席树即可. #include<cstdio& ...
- BZOJ 2466 [中山市选2009]树(高斯消元)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2466 [题目大意] 给定一棵树,每个节点有一盏指示灯和一个按钮.如果节点的按扭被按了, ...
- gearman学习笔记
<?php 搭建描述: . 在实际使用时应该是运行gearmand -d 的 server 一台. [要装gearmand,运行gearmand] . 处理worker的机器若干[要装gearm ...
- python开发_platform_获取操作系统详细信息工具
''' python中,platform模块给我们提供了很多方法去获取操作系统的信息 如: import platform platform.platform() #获取操作系统名称及版本号,'Win ...
- wikioi 1576 最长严格上升子序列
简单的最长严格上升子序列的题 dp[i]表示到a[i]这个数为最后的时候最大的长度是多少 然后就差不多了吧~ #include <cstdio> #include <cmath> ...
- rpm管理环境包和代码包
Author: JinDate: 20140610System: CentOS release 6.5 (Final) 06-09-2014c零:问题配置文件问题,不打包使用的配置文件参考配置文件后缀 ...
- UEFI引导模式
Author: JinDate: 20140827System: windows 刚帮楼下的公司解决了个问题. 原来的办公电脑,预装linux,他们重装成win7.新买的电脑预装成win8,安装出问题 ...
- 理解linux下源码、yum和rpm安装方法的特点
1.yum可看作在线安装,只需yum install 软件名,系统就自动根据yum源配置文件中的镜像位置去下载安装包,并可以自动分析所需的软件依赖关系,自动安装所需的依赖软件包.简单方便,不易出错,不 ...
- 完整的POM文档内容
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...