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. Chap2:二进数值与记数系统[Computer Science Illuminated]

    1 基数(base):记数系统的基本数值,规定了这个系统中使用的数字量和数位位置的值 2 数字采用位置计数法进行编写 位置计数法(positional notation):一种表达数字的系统,数位按顺 ...

  2. kafka相关命令

    查看kafka消费组对应的信息:./kafka-consumer-groups.sh --bootstrap-server 172.17.6.10:9092 --describe --group fr ...

  3. java JDBC (三) 修改

    package cn.sasa.demo3; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Pr ...

  4. linux里source、sh、bash、./有什么区别(转)

    add by zhj: 主要不同是,source是在当前shell中执行脚本,而sh, bash, ./是在当前shell的child shell中执行脚本 原文:http://www.cnblogs ...

  5. 机器学习技术点----apachecn的github地址

    预处理 离散化 等值分箱 等量分箱 独热 one-hot 标准化 最小最大 min-max z-score l2 标准化 归一化 特征选择 ANOVA 信息增益/信息增益率 模型验证 评价指标 回归 ...

  6. es修改指定的field(partial update)

    PUT /index/type/id 创建文档&替换文档,就是一样的语法一般对应到应用程序中,每次的执行流程基本是这样的:1.应用程序发起一个get请求,获取到document,展示到前台界面 ...

  7. Linux ethtool 命令

    ethtool 是用于查询及设置网卡参数的命令,常见用法如下: 注意:该命令只是临时设置,如果网卡重启就失效了,如果想要永久保存应该配置 /etc/sysconfig/network-scripts/ ...

  8. 微信和WeChat合并月活跃数达8.89亿,移动支付月活跃账户超过6亿

    3月22日,腾讯公布2016年年度业绩报告,微信和WeChat合并月活跃用户数达8.89亿,同比增长28%:2016年12月,腾讯移动支付的月活跃账户及日均支付交易笔数均超过6亿.腾讯主席兼首席执行官 ...

  9. Codeforces Round #FF (Div. 2) 题解

    比赛链接:http://codeforces.com/contest/447 A. DZY Loves Hash time limit per test 1 second memory limit p ...

  10. MRPT编译

    今天尝试编译一下MRPT,主要是为了学习里面的路径规划算法. 自主探索,未知环境探索...... 编译的过程中遇到一个问题就是wxWidgets老是检测不到,让我添加它的root目录.明明wxWidg ...