Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine if s can be segmented into a space-separated sequence of one or more dictionary words.

Note:

  • The same word in the dictionary may be reused multiple times in the segmentation.
  • You may assume the dictionary does not contain duplicate words.

Example 1:

Input: s = "leetcode", wordDict = ["leet", "code"]
Output: true
Explanation: Return true because "leetcode" can be segmented as "leet code".

题意:

给定字符串S和字典wordDict, 判断字符串S是否能由字典中的单词组合而成

思路:

用一维boolean数组记录是否能字符串S是否能被分隔

l       e      e      t       c      o      d     e

T F F F F F F F F
0 1 2 3 4 5 6 7 8

dp[0]初始化为true, 其他为default的false

dp[s.length() + 1]

i = 1         j = 1,  dp[1] 为default F, 没必要再去验证wordDict.contains(s.substring(j,i))

j = 0,  dp[0] 为 T , 验证wordDict.contains(s.substring(0,1)) 为F

i = 2        j = 2,  dp[2] 为default F

j = 1,  dp[1] 为default F

      j = 0,  dp[0] 为 T , 验证wordDict.contains(s.substring(0,2)) 为F

i = 3        j = 3,  dp[3] 为default F

j = 2,  dp[2] 为default F

j = 1,  dp[1] 为default F

      j = 0,  dp[0] 为 T , 验证wordDict.contains(s.substring(0,3)) 为F

i = 4       j = 4, dp[4]为default F

     j = 3,  dp[3] 为default F

j = 2,  dp[2] 为default F

j = 1,  dp[1] 为default F

      j = 0,  dp[0] 为 T , 验证wordDict.contains(s.substring(0,4)) 为T  ----> 更新dp[4] = true

T F F F T F F F F
0 1 2 3 4 5 6 7 8

代码:

 class Solution {
public boolean wordBreak(String s, List<String> wordDict) {
// corner case
if (s == null || s.length() < 1 || wordDict == null || wordDict.size() < 1) {
return false;
}
boolean[] dp = new boolean[s.length() + 1];
dp[0] = true;
//外层循环scan字符串S
for(int i = 1; i <= s.length(); i++ ){
//内层循环找分割点
for(int j = i; j >= 0; j--){
if(dp[j] && wordDict.contains(s.substring(j,i))){
dp[i] = true;
}
}
}
return dp[s.length()];
}
}

[leetcode]139. Word Break单词能否拆分的更多相关文章

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

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

  2. LeetCode 139. Word Break单词拆分 (C++)

    题目: Given a non-empty string s and a dictionary wordDict containing a list of non-emptywords, determ ...

  3. leetcode 139. Word Break 、140. Word Break II

    139. Word Break 字符串能否通过划分成词典中的一个或多个单词. 使用动态规划,dp[i]表示当前以第i个位置(在字符串中实际上是i-1)结尾的字符串能否划分成词典中的单词. j表示的是以 ...

  4. Leetcode#139 Word Break

    原题地址 与Word Break II(参见这篇文章)相比,只需要判断是否可行,不需要构造解,简单一些. 依然是动态规划. 代码: bool wordBreak(string s, unordered ...

  5. [LeetCode] 139. Word Break 拆分词句

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

  6. 139 Word Break 单词拆分

    给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,确定 s 是否可以被空格分割为一个或多个在字典里出现的单词.你可以假设字典中无重复的单词.例如,给出s = "leet ...

  7. leetcode 139. Word Break ----- java

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

  8. LeetCode #139. Word Break C#

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

  9. [LeetCode] 139 Word Break(BFS统计层数的方法)

    原题地址: https://leetcode.com/problems/word-break/description/ 题目: Given a non-empty string s and a dic ...

随机推荐

  1. linux设置iptables防火墙的详细步骤(centos防火墙设置方法)

    CentOS系统也是基于linux中的它的防火墙其实就是iptables了,下面我来介绍在CentOS防火墙iptables的配置教程,希望此教程对各位朋友会有所帮助.   iptables是与Lin ...

  2. 关于input=file的用法

    <input type="file"/>这个东西是用来上传图片用的. 1,但是存在一下问题但是在在各个浏览器下的显示是不一样的 IE下: IE之外的浏览器: 2.如果不 ...

  3. javascript的焦点管理

    HTML5也添加了辅助管理DOM焦点的功能. 元素获得焦点的方式有页面加载,用户输入和代码中调用的focus()方法. 而document.activeElement属性保存着当前获得焦点的引用. v ...

  4. HttpClient上传下载文件

    HttpClient上传下载文件 java HttpClient Maven依赖 <dependency> <groupId>org.apache.httpcomponents ...

  5. webpack(3)--Output

    Output output配置如何输出最终想要的代码,output是一个object里面包含一系列配置. 1. filename output.filename配置输出文件的名称,为string类型, ...

  6. systemctl管理系统配置、服务

    systemctl daemon-reload \&& systemctl enable docker \&& systemctl start docker \& ...

  7. Code First use dotConnect for MySQL

    http://www.dotblogs.com.tw/yc421206/archive/2014/03/24/144507.aspx dotConnect for MySQL 是一家強大的 3rd C ...

  8. jquery checkbox

    $(document).ready(function() { var $cr = $("#cr"); var cr = $cr[0]; $cr.click(function() { ...

  9. 面向过程中的局部变量(global)

    1.school = 'oldboy.edu' def change_name(name): school = " Mage Linux"                  # 局 ...

  10. eclipse 乱码

    svn乱码: 教你解决Eclipse中SVN比较乱码问题 workspace->utf-8设置后成功! console乱码: 项目右键 :  run as configuration 设置com ...