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

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

dp[i]  0-i这个字符串是否满足wordbreak

dp[0] = 0

dp[1] =dp[0] && s[0:1] in dict

dp[2] =dp[0] && s[0:1] in dict  ||  dp[1] && s[1:2] in dict

dp[3] =dp[0] && s[0:3] in dict  ||  dp[1] && s[1:3] in dict ||  dp[2] && s[2:3] in dict

dp[i] =dp[0] && s[0:i] in dict || ,,,,,,,||dp[i-1]&& s[i-1:i] in dict


Time complexity O(n^2)

Space complexity O(n)

语法注意:

s = "abcde"; 要得到”ce“

string word = s.substr(2,2);

 class Solution {
public:
bool wordBreak(string s, vector<string>& wordDict) {
vector<bool> dp(s.size()+,false);
unordered_set<string>dict(wordDict.begin(),wordDict.end());
dp[] = true;
for(int i = ; i<=s.size();i++){
for(int j = ;j<i;j++){
string word = s.substr(j,i-j);
if(dp[j]&&dict.find(word)!=dict.end()){
cout<<word<<endl;
dp[i] = true;
break;
}
}
}
return dp[s.size()];
}
};

参考:
http://zxi.mytechroad.com/blog/leetcode/leetcode-139-word-break/

139. Word Break(动态规划)的更多相关文章

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

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

  2. 139. Word Break 以及 140.Word Break II

    139. Word Break Given a non-empty string s and a dictionary wordDict containing a list of non-empty  ...

  3. Leetcode#139 Word Break

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

  4. 139. Word Break

    题目: Given a string s and a dictionary of words dict, determine if s can be segmented into a space-se ...

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

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

  6. 【LeetCode】139. Word Break 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  7. 动态规划之139 Word Break

    题目链接:https://leetcode-cn.com/problems/word-break/ 参考链接:https://blog.csdn.net/c_flybird/article/detai ...

  8. Word Break(动态规划)

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

随机推荐

  1. mysql分库 分表

    原文链接:http://www.jianshu.com/p/89311703b320 传统的分库分表传统的分库分表都是通过应用层逻辑实现的,对于数据库层面来说,都是普通的表和库.分库分库的原因 首先, ...

  2. Matlab中导入文本文件中的数据 矩阵合并 以及C++中删除文件操作

    今天用到了Matlab读取文本文件中按照一定格式存储好的数据,然后进行后续的分析计算等,因此涉及到Matlab的文件读取,记录在这里,供以后查阅: fid = fopen('train.set', ' ...

  3. vue-router重新定向、redirect与alias区别

    redirect app.vue <router-link to="/goParams/918/i like vue">goParams</router-link ...

  4. SSH高级服务

    SSH端口转发 SSH 会自动加密和解密所有 SSH 客户端与服务端之间的网络数据.但是,SSH 还能够将其他 TCP 端口的网络数据通过 SSH 链接来转发,并且自动提供了相应的 加密及解密服务.这 ...

  5. XML 文档(1, 2)中有错误:不应有 <xml xmlns=''>

    症状 用XmlSerializer进行xml反序列化的时候,程序报错: 不应有 <xml xmlns=''>. 说明: 执行当前 Web 请求期间,出现未经处理的异常.请检查堆栈跟踪信息, ...

  6. 帝国cms建站-动态获取栏目id

    <?php $fcr=explode('|',$class_r[$GLOBALS[navclassid]][featherclass]); $topbclassid=$fcr[1]?$fcr[1 ...

  7. css中 ~的作用

    这是 CSS3 element1~element2 选择器 定义和用法 element1~element2 选择器 element1 之后出现的所有 element2. 两种元素必须拥有相同的父元素, ...

  8. 微信小程序 - 表单验证插件WxValidate使用

    插件下载地址及官方文档:https://github.com/skyvow/wx-extend 具体的WxValidate.js文件的位置在wx-extend/src/assets/plugins/w ...

  9. 将c语言的结构体定义变成对应的golang语言的结构体定义,并将golang语言结构体变量的指针传递给c语言,cast C struct to Go struct

    https://groups.google.com/forum/#!topic/golang-nuts/JkvR4dQy9t4 https://golang.org/misc/cgo/gmp/gmp. ...

  10. 使用MySQL的mysqldump命令备份数据库和把数据库备份文件恢复

    1,备份数据库 mysql -uroot -p123456 db_name > /root/db_name.dump 2,数据库备份文件恢复 mysql -uroot -p123456 db_n ...