原题地址

与Word Break II(参见这篇文章)相比,只需要判断是否可行,不需要构造解,简单一些。

依然是动态规划。

代码:

 bool wordBreak(string s, unordered_set<string> &dict) {
int maxLen = ;
for (auto w : dict)
maxLen = maxLen > w.length() ? maxLen : w.length(); vector<bool> res(s.length() + , false);
res[s.length()] = true; for (int i = s.length() - ; i >= ; i--) {
for (int l = ; !res[i] && l <= maxLen && i + l <= s.length(); l++)
res[i] = dict.find(s.substr(i, l)) != dict.end() && res[i + l];
} return res[];
}

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 、140. Word Break II

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

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

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

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

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

  6. [leetcode]139. Word Break单词能否拆分

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

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

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

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

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

  9. Java for LeetCode 139 Word Break

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

随机推荐

  1. form表单无刷新提交文件(iframe)

    先看一段代码(PHP例子) 1.表单代码(form.html): <iframe name="testIframeName" style="display:none ...

  2. PHP导出excel文件

    现在教教你如何导入excel文件: 在我的文件储存里面有一个com文件夹的,将其解压放在ThinkPHP/Library/文件夹里面,然后就是写控制器啦!去调用这个插件: <?php names ...

  3. php判断是否为json格式的方法

    php判断是否为json格式的方法. 首先要记住json_encode返回的是字符串, 而json_decode返回的是对象 判断数据不是JSON格式: 复制代码代码如下: function is_n ...

  4. Mysql查询(笔记二)

    1.两结构相同的表数据间移植 Inset into 表一 Select 字段1,字段2,....字段n from表二 建立数据库时设置数据库编码 create database 数据库名 charse ...

  5. WebForm与MVC混用

    步骤一:添加引用 -> 程序集 -> 扩展 -> System.Web.Mvc ; System.Web.Razor; System.Web.WebPages; System.Web ...

  6. lldb

    所有命令选择与input 值用  -- 区分 1 p/x 16 转16进制 https://sourceware.org/gdb/onlinedocs/gdb/Output-Formats.html ...

  7. java的软件包

    Java的软件包:简单来说,软件包就是把类放在不同的文件夹下,提供了命名空间 package wang; //用package将Test类放在wang文件下 class Test{ public st ...

  8. 菜鸟学习Hibernate——持久层框架

    一.Java操作数据库的阶段. Java对数据库进行操作经历了三个阶段. 1.1操作JDBC阶段 这个阶段就是利用JDBC类来操作数据库.这个阶段出现了两个问题: 代码过度重复:在每一次数据库操作的是 ...

  9. hdu 4198 Quick out of the Harbour

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4198 Quick out of the Harbour Description Captain Cle ...

  10. ios 中怎么自定义(RGB)背景色

    1.定义RGB 色彩.随机颜色 我的抽为宏定义.便于各个文件中使用 // 1.获得RGB颜色 #define MTColor(r, g, b) [UIColor colorWithRed:(r)/25 ...