题目:

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:

  1. Input: s = "leetcode", wordDict = ["leet", "code"]
  2. Output: true
  3. Explanation: Return true because "leetcode" can be segmented as "leet code".

Example 2:

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

Example 3:

  1. Input: s = "catsandog", wordDict = ["cats", "dog", "sand", "and", "cat"]
  2. 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,是为了方便计算。

程序:

  1. class Solution {
  2. public:
  3. bool wordBreak(string s, vector<string>& wordDict) {
  4. unordered_set<string> set_s(wordDict.begin(), wordDict.end());
  5. vector<int> dp(s.size()+, );
  6. dp[] = ;
  7. for(int i = ; i < dp.size(); ++i){
  8. for(int j = ; j <= i; ++j){
  9. string it = s.substr(j, i-j);
  10. if(set_s.count(it) && dp[j]){
  11. dp[i] = ;
  12. break;
  13. }
  14. }
  15. }
  16. if(dp.back()) return true;
  17. else return false;
  18. }
  19. };

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. Ubuntu命令行打开新终端并执行脚本

    gnome-terminal -x bash -c "bash test.sh;exec bash;"

  2. Paper | Model-blind video denoising via frame-to-frame training

    目录 故事 本文方法 流程 训练 实验 发表在2019年CVPR. 核心内容:基于Noise2Noise思想,这篇文章致力于无监督的视频盲去噪:是的,连噪声样本都不需要了. 这篇文章写作和概括太棒了! ...

  3. angular修改端口号port

    报错:Port 4200 is already in use. Use '--port' to specify a different port. 因为4200端口已被使用,请使用“--port”修改 ...

  4. Java连载8-基本数据类型2

    一.基本数据类型 1.字符串“abc”不属于基本数据类型,属于引用数据类型 2. 基本数据类型   占用空间大小(单位:字节) byte                1 short          ...

  5. 大话设计模式Python实现-工厂方法模式

    工厂方法模式(Factory Method Pattern):定义一个用于创建对象的接口,让子类决定实例化哪一个类,工厂方法使一个类的实例化延时到其子类. #!/usr/bin/env python ...

  6. 我在生产项目里是如何使用Redis发布订阅的?(一)使用场景

    转载请注明出处! 导语 Redis是我们很常用的一款nosql数据库产品,我们通常会用Redis来配合关系型数据库一起使用,弥补关系型数据库的不足. 其中,Redis的发布订阅功能也是它的一大亮点.虽 ...

  7. idea 项目在一般模式下可以正常启动,在debug模式下无法启动,像是卡住了的感觉

    项目一般模式下可以启动,debug模式下就是启动不了,后经过排查发现打的有断点,断点取消在重启立马就可以啦. Method breakpoints may dramatically slow down ...

  8. runtime template in vuejs

    在使用vuejs开发的过程中,有时候我们需要动态模板的场景,也就是说模板并不是静态定义好的,而是动态变化的. 比如在做一个所见所得编辑器时,编辑人员可能时刻需要调整他设计的页面内容,而如果页面内容包含 ...

  9. CSS 基础面试题

    1 介绍一下标准的CSS的盒子模型?与低版本IE的盒子模型有什么不同的? 标准盒子模型:宽度=内容的宽度(content)+ border + padding + margin 低版本IE盒子模型:宽 ...

  10. 新mac 下第一次 安装 mongodb 步骤

    新入手mac,安装mongo步骤记录:不建议使用网上的brew安装方法,因为试了半天没有成功,应该是新版本限制比较多! 从mongodb官网下载mac版本mongo: 1.访问MongoDB官方下载地 ...