题目:

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. 微信小程序-自定义方法的抛出与引用

    一. 定义方法与抛出(utils/foo.js文件中) function say () { console.log('自定义的say方法')}  # 定义方法 module.exports = {sa ...

  2. 内网Metasploit映射到外网

    下载frp Github项目地址:https://github.com/fatedier/frp 找到最新的releases下载,系统版本自行确认. 下载方法: wget https://github ...

  3. Wamp 下运行 CGI 笔记

    虽然假期的余额不足了,但是仔细想想放假又有多少事情可以做呢?休息的差不多了,还是上班的好,长时间的休息人就废了.同意的举手,不同意的就算了. httpd.conf 的配置 我这里使用的是 Wamp 的 ...

  4. 移动端js触摸touch详解(附带案例源码)

    移动端触摸滑动原理详解案例,实现过程通过添加DOM标签的触摸事件监听,并计算触摸距离,通过距离坐标计算触摸角度,最后通过触摸角度去判断往哪个方向触摸的. 触摸的事件列表 触摸的4个事件: touchs ...

  5. 修改 Oracle 数据库实例字符集

    Ø  简介 在 Oracle 中创建数据库实例后,就会有对应使用的编码字符集.当我们设置的字符集与操作系统或者其他软件字符集不一致时,就会出现个字符长度存储一个汉字. 2.   SIMPLIFIED ...

  6. C# 中一个限制 Task 并发执行的数量的示例

    直接贴代码了: using System; using System.Linq; using System.Threading; using System.Threading.Tasks; class ...

  7. Window权限维持(六):BITS Jobs

    Windows操作系统包含各种实用程序,系统管理员可以使用它们来执行各种任务.这些实用程序之一是后台智能传输服务(BITS),它可以促进文件到Web服务器(HTTP)和共享文件夹(SMB)的传输能力. ...

  8. Elasticsearch 在业界的大量应用案例

    国内现在有大量的公司都在使用 Elasticsearch,包括携程.滴滴.今日头条.饿了么.360安全.小米.vivo等诸多知名公司.

  9. Python - MySQL 数据库连接 - PyMySQL 驱动 - 第二十五天

    序言 本文我们为大家介绍 Python3 使用 PyMySQL 连接数据库,并实现简单的增删改查. 什么是 PyMySQL? PyMySQL 是在 Python3.x 版本中用于连接 MySQL 服务 ...

  10. 十三道Python练习题

    一.完美立方 编写一个程序,对任给的正整数N (N≤100),寻找所有的四元组(a, b, c, d),使得a^3= b^3 + c^3 + d^3,其中a,b,c,d 大于 1, 小于等于N. 输入 ...