题目:

Given a non-empty string s and a dictionary wordDict containing a list of non-emptywords, 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".

Example 2:

Input: s = "applepenapple", wordDict = ["apple", "pen"]
Output: true
Explanation: Return true because "applepenapple" can be segmented as "apple pen apple".
  Note that you are allowed to reuse a dictionary word.

Example 3:

Input: s = "catsandog", wordDict = ["cats", "dog", "sand", "and", "cat"]
Output: false

分析:

给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,判定 s 是否可以被空格拆分为一个或多个在字典中出现的单词。

如果使用搜索的话应该是会超时的,这里使用动态规划的方法。

dp[i]表示字符串s的前i个字符能否被拆分,使用substr来截取字符串。

    l e e t c o d e
dp 1 0 0 0 1 0 0 0 ?

假如我们现在判断dp[i]的值,我们要同时判断dp[j]==1和后面的字符串(substr(j, i-j))是否在字典中是不是同时成立的。

比如现在要求dp[8]的值,j=0时,dp[0]==1但substr(0, 8-0)也就是leetcode不在字典中,当j=4时,dp[4]==1且substr(4, 8-4)也就是code在字典中,所以dp[8]赋值1。最后返回dp中最后的值即可。

    l e e t c o d e
dp 1 0 0 0 1 0 0 0 1

注:在dp的长度要比字符串长度多1,是为了方便计算。

程序:

class Solution {
public:
bool wordBreak(string s, vector<string>& wordDict) {
unordered_set<string> set_s(wordDict.begin(), wordDict.end());
vector<int> dp(s.size()+, );
dp[] = ;
for(int i = ; i < dp.size(); ++i){
for(int j = ; j <= i; ++j){
string it = s.substr(j, i-j);
if(set_s.count(it) && dp[j]){
dp[i] = ;
break;
}
}
}
if(dp.back()) return true;
else return false;
}
};

LeetCode 139. Word Break单词拆分 (C++)的更多相关文章

  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单词能否拆分

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

  3. 139 Word Break 单词拆分

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

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

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

  5. Leetcode#139 Word Break

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

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

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

  7. Leetcode139. Word Break单词拆分

    给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,判定 s 是否可以被空格拆分为一个或多个在字典中出现的单词. 说明: 拆分时可以重复使用字典中的单词. 你可以假设字典中没有重复 ...

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

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

随机推荐

  1. CSP-J&S2019第二轮游记认证

    Day 0 我毕竟不是竞赛省,在黑龙江这个弱省任何初中都没有竞赛生的----在初中,文化课第一----永远如此. 因而,我并不能翘掉周五的文化课来复习或是提前前往省城参加下午2:00~6:00的试机. ...

  2. 阿里云cdn缓存设置技巧,不同文件结尾用不同的缓存时间

    https://edu.aliyun.com/lesson_130_1505?spm=5176.10731542.0.0.2ed37dbf42YL6U#_1505

  3. ubuntu / zsh shell / oh-my-zsh / 常用插件

    记录一下 zsh 的下载与配置,省得每次重装系统都要上网到处查. 安装 zsh shell sudo apt install zsh 切换 shell chsh -s /bin/zsh 安装 oh-m ...

  4. AutoDesk公司搞的fbx模型格式

    FBX® data exchange technology is a 3D asset exchange format that facilitates higher-fidelity data ex ...

  5. POJ-2661Factstone Benchmark

    Factstone Benchmark Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5577   Accepted: 25 ...

  6. Prometheus 监控K8S集群资源监控

    Prometheus 监控K8S集群中Pod 目前cAdvisor集成到了kubelet组件内,可以在kubernetes集群中每个启动了kubelet的节点使用cAdvisor提供的metrics接 ...

  7. VMware+windbg时快照功能的使用

    VMware+windbg时快照功能的使用 一.快照功能的使用 我们在编写与调试驱动文件时经常会面临死机蓝屏情况,如果像平常一样重启,再重新配置环境,肯定会占用大量时间. Vmware虚拟机给我们提供 ...

  8. Microsoft.Practices.Unity

    // // Summary: // Register a type mapping with the container. // // Parameters: // container: // Con ...

  9. 洛谷 p1008三连击

    洛谷 p1008三连击 题目背景 本题为提交答案题,您可以写程序或手算在本机上算出答案后,直接提交答案文本,也可提交答案生成程序. 题目描述 将1,2, ⋯,9共99个数分成3组,分别组成3个三位数, ...

  10. fetch和XMLHttpRequest

    fetch和XMLHttpRequest 如果看网上的fetch教程,会首先对比XMLHttpRequest和fetch的优劣,然后引出一堆看了很快会忘记的内容(本人记性不好).因此,我写一篇关于fe ...