Word Break 题解

原创文章,拒绝转载

题目来源:https://leetcode.com/problems/word-break/description/


Description

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. You may assume the dictionary does not contain duplicate words.

For example, given

s = "leetcode",

dict = ["leet", "code"].

Return true because "leetcode" can be segmented as "leet code".

Solution

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

解题描述

这道题采用的是动态规划的方式进行求解。将整个问题化成小问题:生成来源字符串的所有子串,并在词典中查找字串,如果存在则进行标记,之后查看所有的这些字串能否连起来组成原来的字符串。

[Leetcode Week9]Word Break的更多相关文章

  1. [Leetcode Week9]Word Break II

    Word Break II 题解 题目来源:https://leetcode.com/problems/word-break-ii/description/ Description Given a n ...

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

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

  3. [LeetCode] 140. Word Break II 单词拆分II

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

  4. 【leetcode】Word Break (middle)

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

  5. 【leetcode】Word Break II

    Word Break II Given a string s and a dictionary of words dict, add spaces in s to construct a senten ...

  6. Leetcode#139 Word Break

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

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

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

  8. LeetCode 139. Word Break单词拆分 (C++)

    题目: Given a non-empty string s and a dictionary wordDict containing a list of non-emptywords, determ ...

  9. leetcode 140. Word Break II ----- java

    Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...

随机推荐

  1. python csv 模块的使用

    python csv 模块的使用 歌曲推荐:攀登(live) csv 是用逗号分隔符来分隔列与列之间的. 1. csv的写入 1.简单的写入,一次写入一行 import csv with open(& ...

  2. (原)自定义资源预览工具:DZAssetPreviewPlugin(1)

    @author: 白袍小道 转载请说明,谢谢     题记 后续工具制作单独作为一本(小道用的是OneNote,这样发布简单点.*--*) 总计放到写完后.     目的 1.快速预览资源(因为大部分 ...

  3. 剑指offer:跳台阶

    目录 题目 解题思路 具体代码 题目 题目链接 剑指offer:跳台阶 题目描述 一只青蛙一次可以跳上1级台阶,也可以跳上2级.求该青蛙跳上一个n级的台阶总共有多少种跳法(先后次序不同算不同的结果). ...

  4. 关于如何利用原生js动态给一个空对象添加属性以及属性值

    首先,回忆一下,访问对象属性一共有两种方法:点获取法和方括号获取法.而我们最常用的就是点获取法了.但是当我们遇到需要给对象动态添加属性和属性值时,点获取法好像就不太好用了,尤其是我们不知道属性名的时候 ...

  5. RunKit & NPM

    RunKit + NPM Try any Node.js package right in your browser https://npm.runkit.com/segmentit

  6. php 文件上传失败

    使用OSX系统,在使用MAMP Pro作为虚拟服务器,并使用PHP作为后端语言进行文件上传,从临时文件夹拷贝文件的方法为 move_uploaded_file 代码如下: if($_FILES['fi ...

  7. Hibernate关联映射之_一对多

    domain: package org.zln.hibernate.domain; import java.util.Set; /** * 部门Domain对象 * Created by sherry ...

  8. 【bzoj2815】[ZJOI2012]灾难 拓扑排序+倍增LCA

    题目描述(转自洛谷) 阿米巴是小强的好朋友. 阿米巴和小强在草原上捉蚂蚱.小强突然想,果蚂蚱被他们捉灭绝了,那么吃蚂蚱的小鸟就会饿死,而捕食小鸟的猛禽也会跟着灭绝,从而引发一系列的生态灾难. 学过生物 ...

  9. BZOJ4345 POI2016Korale(构造+堆+线段树)

    注意到k与n同阶,考虑构造一种枚举子集的方式,使得尽量先枚举较小的子集.首先sort一下,用堆维护待选子集.每次取出最小子集,并加入:1.将子集中最大数ai替换为ai+1 2.直接向子集中添加ai+1 ...

  10. Eclipse打不开,闪退

    自己编写了个程序,运行巨慢..无语,输出太多,后来冒出一个错误,不知什么原因啊,再后来Eclipse就打不开了,到workbench闪退... 百度后解决方案: 进入目录:workspace/.met ...