Leetcode#139 Word Break
与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的更多相关文章
- [LeetCode] 139. Word Break 单词拆分
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...
- leetcode 139. Word Break 、140. Word Break II
139. Word Break 字符串能否通过划分成词典中的一个或多个单词. 使用动态规划,dp[i]表示当前以第i个位置(在字符串中实际上是i-1)结尾的字符串能否划分成词典中的单词. j表示的是以 ...
- LeetCode 139. Word Break单词拆分 (C++)
题目: Given a non-empty string s and a dictionary wordDict containing a list of non-emptywords, determ ...
- 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 ...
- 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 ...
- [leetcode]139. Word Break单词能否拆分
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...
- [LeetCode] 139 Word Break(BFS统计层数的方法)
原题地址: https://leetcode.com/problems/word-break/description/ 题目: Given a non-empty string s and a dic ...
- [LeetCode] 139. Word Break 拆分词句
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...
- 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 ...
随机推荐
- Silverlight取得Session
首先Session是运行在服务器上的,而Silverlight运行在客户端.因此在Silverlight中使用SESSION的说法并不准确, 只因大家经常这样搜索才起这个名字. 有两种方法实现Silv ...
- php常用函数集锦[备份用的]
1.判断是否正确的日期格式 /** * 是否正确的日期 * * @access public */ private function _isdate($str,$format="Y-m-d ...
- linux下的mount命令的用法详解
挂接命令(mount) 首先,介绍一下挂接(mount)命令的使用方法,mount命令参数非常多,这里主要讲一下今天我们要用到的. 命令格式:mount [-t vfstype] [-o option ...
- ios 工程图片清理shell
#!/bin/shecho "随意删除@2x图片可能会引起错误 因为ios工程会更加前缀和分辨率自己找到@2x的图片 所以删除@2x图片时要慎重"read -n1 -p &quo ...
- Moses创建一个翻译系统的基本过程记录,以后会按照每个过程详细说明,并给出每个步骤的参数说明
软件需求: 首先你必须要有Moses(废话哈哈).然后要有GIZA++用作词对齐(traning-model.perl的时候会用到).IRSTLM产生语言模型 大致步骤: 大体的步骤如下: 准备Par ...
- 刀哥多线程之调度组gcd-12-group
调度组 常规用法 - (void)group1 { // 1. 调度组 dispatch_group_t group = dispatch_group_create(); // 2. 队列 dispa ...
- poj 3259 Wormholes
题目连接 http://poj.org/problem?id=3259 Wormholes Description While exploring his many farms, Farmer Joh ...
- Douglas Crockford: entityify & deentityify
大神之字符与字符实体的相互转换方法 // & to & if (!String.prototype.entityify) { String.prototype.entityify = ...
- Linux开机启动程序详解
Linux开机启动程序详解我们假设大家已经熟悉其它操作系统的引导过程,了解硬件的自检引导步骤,就只从Linux操作系统的引导加载程序(对个人电脑而言通常是LILO)开始,介绍Linux开机引导的步骤. ...
- Go时间戳和日期字符串的相互转换
Go语言中,获取时间戳用time.Now().Unix(),格式化时间用t.Format,解析时间用time.Parse. 看实例代码: package main import ( "fmt ...