题目:

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的更多相关文章

  1. 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 ...

  2. Word Break - LeetCode

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

  3. Word Ladder leetcode java

    题目: Given two words (start and end), and a dictionary, find the length of shortest transformation se ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. LeetCode Word Break II

    原题链接在这里:https://leetcode.com/problems/word-break-ii/ 题目: Given a string s and a dictionary of words  ...

  8. [LeetCode] 139. Word Break 单词拆分

    Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...

  9. [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 ...

随机推荐

  1. Noip模拟题 Matrix [递推,组合数]

    Matrix 时间限制: 1 Sec  内存限制: 512 MB 题目描述 小 z 的女朋友送给小 z 一个 n × n 的矩阵.但是矩阵实在太大了,小 z 的女朋友拿不动,只能带给他两个长度为 n ...

  2. 《Multi-Agent Actor-Critic for Mixed Cooperative-Competitive Environments》论文解读

    MADDPG原文链接 OpenAI bog DDPG链接 目录 一.摘要 二.效果展示 三.方法细节 问题分析 具体方法 伪代码 网络结构 四.实验结果 五.总结 附录 Proposition 1 一 ...

  3. [leetcode tree]107. Binary Tree Level Order Traversal II

    Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...

  4. map赋值前要先初始化:assignment to entry in nil map

    注意这种map的嵌套的形式,make只初始化了map[string]T部分(T为map[int]int),所以下面的赋值会出现错误: test := make(map[string]map[int]i ...

  5. 详解Python中的__init__和__new__(静态方法)

    一.__init__ 方法是什么? 使用Python写过面向对象的代码的同学,可能对 __init__ 方法已经非常熟悉了,__init__ 方法通常用在初始化一个类实例的时候.例如: #-*- co ...

  6. python opencv3 运动检测

    git:https://github.com/linyi0604/Computer-Vision 思路:  开启摄像头后 设置一个当前帧为背景, 在之后检测到的帧都与背景对比不同,对不同的地方进行检测 ...

  7. 【BZOJ 1419】1419: Red is good (概率DP)

    1419: Red is good Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 807  Solved: 343 Description 桌面上有R张 ...

  8. 「HNOI2018」毒瘤

    「HNOI2018」毒瘤 解题思路 先考虑只有一棵树的情况,经典独立集计数. \[ dp[u][0]=\prod (dp[v][0]+dp[v][1]) \\ dp[u][1]=\prod dp[v] ...

  9. 轻巧的编辑器:Sublime Text3 user设置

    开发到现在,编辑器倒用过不少,VIM.zend.my eclipse.EPP.editplus.notepad++.sublime text 2. 最初使用sublime是同学推荐的,说其何其的好,何 ...

  10. ROS知识(14)----局部避障的动态窗口算法(DWA)及其调试的方法

    Dynamic Window Approach(DWA)是重要的局部轨迹规划算法,ROS中使用了DWA算法获得了很好的局部路径规划的效果.具体的教程可参考官方的导航调试资料Navigation Tun ...