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 ...
 
随机推荐
- java_String、StringBuilder
			
在介绍String和StringBuilder前先学习一下equals方法和toString方法.API java1.6提取码:04b6 equals方法 equals方法,用于比较两个对象是否相同, ...
 - 美团外卖iOS多端复用的推动、支撑与思考
			
背景 美团外卖2013年11月开始起步,随后高速发展,不断刷新多项行业记录.截止至2018年5月19日,日订单量峰值已超过2000万,是全球规模最大的外卖平台.业务的快速发展对技术支撑提出了更高的要求 ...
 - 哪种写法更好?<script></script> vs/or <script type=”text/javasript”></script>
			
一直很奇怪 哪种写法更好<script type=“text/javascript”>…</script> or <script>…</script>? ...
 - C# 复制、粘贴文本信息到系统剪贴板
			
复制: Clipboard.SetDataObject(textBox1.SelectedText); 粘贴: IDataObject iData = Clipboard.GetDataObject( ...
 - SQLSERVER2014集群实战——IP引发的坑
			
在之前的帖子里有提到过,为了避免IP变更带来的一系列问题,采取了不改变IP的策略.原以为,只要SQLSERVER的群集IP保持与之前单机部署的IP一致,就基本上不会有问题.然而实际永远比预想的更复杂. ...
 - sgu 261
			
学习了元根的一些知识,哈哈. 总结一下: 几个概念: 阶:对于模数m和整数a,并且gcd(m,a)==1,那么定义a在模m下的阶r为满足ar=1 mod m的最小正整数. 性质1:r in [1,ph ...
 - Sql server 存储过程基础语法
			
一.定义变量 --简单赋值 declare @a int print @a --使用select语句赋值 declare @user1 nvarchar() select @user1='张三' pr ...
 - tsinsen A1067. Fibonacci数列整除问题 dp
			
A1067. Fibonacci数列整除问题 时间限制:1.0s 内存限制:512.0MB 总提交次数:2796 AC次数:496 平均分:51.83 将本题分享到: 查看未格 ...
 - 读书笔记_Effective_C++_条款三十六:绝不重新定义继承而来的non-virtual函数
			
这个条款的内容很简单,见下面的示例: class BaseClass { public: void NonVirtualFunction() { cout << "BaseCla ...
 - 磁盘满了MySQL会做什么?
			
最近遇到一个故障和磁盘满有关系,并且同事也发现经常有磁盘满导致操作hang住无响应的情况,于是抽时间研究了一下这2种情况. 一.磁盘满了之后MySQL会做什么? 我们看下官方的说法 When a di ...