Leetcode139. Word Break单词拆分
给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,判定 s 是否可以被空格拆分为一个或多个在字典中出现的单词。
说明:
- 拆分时可以重复使用字典中的单词。
- 你可以假设字典中没有重复的单词。
示例 1:
输入: s = "leetcode", wordDict = ["leet", "code"] 输出: true 解释: 返回 true 因为 "leetcode" 可以被拆分成 "leet code"。
示例 2:
输入: s = "applepenapple", wordDict = ["apple", "pen"] 输出: true 解释: 返回 true 因为 "applepenapple" 可以被拆分成 "apple pen apple"。 注意你可以重复使用字典中的单词。
示例 3:
输入: s = "catsandog", wordDict = ["cats", "dog", "sand", "and", "cat"] 输出: false
dp[i]表示长为i的从s的第一个字符开始的字串是否能匹配。
可以想为长度为i是否匹配需要看长度i - j是否匹配和是否有长度为j且合适正确的单词。
这样就形成了动态规划的公式。
class Solution {
public:
bool wordBreak(string s, vector<string>& wordDict)
{
int len = s.size();
vector<bool> dp(len + 1, false);
map<string, bool> check;
//i表示结束的位置,相当于end()方法;
for(int i = 0; i < wordDict.size(); i++)
{
check[wordDict[i]] = true;
}
dp[0] = true;
for(int i = 1; i <= len; i++)
{
for(int j = 0; j < i; j++)
{
if(dp[j] && check[string(s.begin() + j, s.begin() + i)])
{
dp[i] = true;
break;
}
}
}
return dp[len];
}
};
Leetcode139. 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单词拆分 (C++)
题目: Given a non-empty string s and a dictionary wordDict containing a list of non-emptywords, determ ...
- 139 Word Break 单词拆分
给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,确定 s 是否可以被空格分割为一个或多个在字典里出现的单词.你可以假设字典中无重复的单词.例如,给出s = "leet ...
- [LeetCode] Word Break II 拆分词句之二
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...
- [Leetcode] word break ii拆分词语
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...
- [leetcode]139. Word Break单词能否拆分
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...
- LeetCode139:Word Break
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- Java Word Break(单词拆解)
给定一个字符串 String s = "leetcode" dict = ["leet", "code"]. 查看一下是够是字典中的词语组成 ...
- [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 ...
随机推荐
- php 引用文件
require_once :为了避免重复加载文件. 用意:加载文件一次. require_once() 语句在脚本执行期间包括并运行指定文件.此行为和require()语句类似,唯一区别是:如果该文件 ...
- 三模数NTT模板
求两个多项式的卷积对任意数p取模 两个好记的FNT模数: 5*2^25+1 7*2^26+1 原根都为3 //Achen #include<algorithm> #include<i ...
- [CTSC 2012]熟悉的文章
二分+单调队列优化dp+后缀自动机 //CTSC2012 熟悉的文章 #include <bits/stdc++.h> using namespace std; const int max ...
- JavaScript基础的一些小总结
一.js变量 1.变量声明: var 关键字来进行变量声明 变量是弱类型 1.数字 2.小数 3.boolean 4.字符串 4.字符 验证数据类型:1.整数,小数是numbe ...
- 使用CEfSharp之旅(5)CEFSharp 隔离Cookie
原文:使用CEfSharp之旅(5)CEFSharp 隔离Cookie 版权声明:本文为博主原创文章,未经博主允许不得转载.可点击关注博主 ,不明白的进群191065815 我的群里问 https:/ ...
- wpf 几种常用控件样式
转自:http://blog.csdn.net/xuejiren/article/details/39449515
- ASP.NET WEB API 特性路由
一.什么是特性路由? 特性路由是指将RouteAttribute或自定义继承自RouteAttribute的特性类标记在控制器或ACTION上,同时指定路由Url字符串,从而实现路由映射,相比之前的通 ...
- layui+croppers完成图片剪切上传
不多说直接上代码: 前台代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8" / ...
- [NOIP2019模拟赛]HC1147 时空阵
题目描述: 幽香这几天学习了魔法,准备建造一个大型的时空传送阵. 幽香现在可以在幻想乡的n个地点建造一些传送门,如果她建造了从地点a与地点b之间的传送门,那么从a到b和从b到a都只需要单位1的时间. ...
- Leetcode187. Repeated DNA Sequences重复的DNA序列
所有 DNA 由一系列缩写为 A,C,G 和 T 的核苷酸组成,例如:"ACGAATTCCG".在研究 DNA 时,识别 DNA 中的重复序列有时会对研究非常有帮助. 编写一个函数 ...