题目说明

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


题目分析

这道题的意思是判断字符串能否由字典里的单词组成,一开始只是以为将字符串分成两部分就可以,进行一些样例测试后才发现可以拆成任意数量的单词,只好重写。

个人思路很简单:

  1. 将字典里的单词一一和字符串进行比对,将符合的索引区间存储在map里,这样就把字典的单词转化成了不同的区间信息;
  2. 建立长度为字符串size+1的bitmap,将第0位置为1,并开始遍历bitmap,做如下操作:假如某一位为1,将所有左端为该位的区间的右端在bitmap中置为1;否则直接跳过。

以下为个人实现(C++ 4ms):

class Solution {
public:
map<int, vector<int>> intervals; void addInterval(string s, string word) {
int left, right;
if (word.size() == 0 || s.size() == 0) return;
for (int i = 0; i < s.size(); i++) {
if (s[i] == word[0]) { // hit first letter
int j;
for (j = 1; i + j < s.size() && j < word.size() && s[i + j] == word[j]; j++); // check
if (j == word.size()) { // match!
intervals[i].push_back(i + j);
}
}
}
} bool wordBreak(string s, vector<string>& wordDict) {
bool bitmap[s.size() + 1] = {true};
for (int i = 0; i < wordDict.size(); i++) {
addInterval(s, wordDict[i]);
}
for (int i = 0; i < s.size(); i++) {
if (bitmap[i]) {
for (int j = 0; j < intervals[i].size(); j++) {
bitmap[intervals[i][j]] = true;
}
}
}
return bitmap[s.size()];
}
};

LeetCode题解:(139) Word Break的更多相关文章

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

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

  2. 【LeetCode】139 - Word Break

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

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

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

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

  5. 【LeetCode】140. Word Break II 解题报告(Python & C++)

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

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

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

  7. Leetcode#139 Word Break

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

  8. 139. Word Break

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

  9. 【LeetCode OJ】Word Break II

    Problem link: http://oj.leetcode.com/problems/word-break-ii/ This problem is some extension of the w ...

  10. 【LeetCode】140. Word Break II

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

随机推荐

  1. Tarjan/2-SAT学习笔记

    Tarjan/2-SAT Tags:图论 作业部落 评论地址 Tarjan 用来求割边或者割点,求点双联通分量或者边双联通分量 点双联通分量:两个点之间有两条点不相交的路径 边双联通分量:两个点之间有 ...

  2. python基础学习1-json,pickle的序列化和反序列化

    import requests import json dic={'k1':'v1'} print(dic,type(dic)) result = json.dumps(dic)#调用dumps方法把 ...

  3. kali base64命令的使用

    1.启动终端 2.输入base64 3.输入需要编码的字符串 4,CTRL+D,结束输入,在屏幕上回显输出结果

  4. [TJOI2014]Alice and Bob[拓扑排序+贪心]

    题意 给出一个序列的以每一项结尾的 \(LIS\) 的长度a[],求一个序列,使得以每一项为开头的最长下降子序列的长度之和最大. \(n\leq 10^5\) . 分析 最优解一定是一个排列,因为如果 ...

  5. 第一道防线__SpringMVC配置拦截器

    这几天在公司自己开发一个小系统,但是系统的安全性也得考虑,起初没注意,赶急就光关心业务逻辑和实现效果.最后老大一出手,就把最严重的问题指出来了,他说你这根本没安全性可言,于是我试着将公司使用的spri ...

  6. hdu1754 I Hate It(线段树单点更新,区间查询)

    传送门 有更新单个学生成绩和查询某个区间内学生成绩最大值两种操作 线段树代码 #include<bits/stdc++.h> using namespace std; +; using n ...

  7. PHP 用户密码加密函数password_hash

    传统的用户名和密码都采用加盐的方式存储加密信息,盐值也需要存储. 自PHP5.5.0之后,新增加了密码散列算法函数(password_hash),password_hash() 使用足够强度的单向散列 ...

  8. 初识IT行业,人生苦短,我学python

    第一次写,我也不知道该怎么写.只有慢慢的去体会大神们的见解与看法. Python是一个较强的脚本语言,而Java是强类型的编程语言.为了更好的入门,我没有去选择强类型语言的Java,而选择使用Pyth ...

  9. 执行sh脚本报“/usr/bin/env: "sh\r": 没有那个文件或目录”错误

    出现这个错误的原因是出错的语句后面多了“\r”这个字符,换言之,脚本文件格式的问题,我们只需要把格式改成unix即可: vi xx.sh :set ff :set ff=unix :wq!

  10. Linux命令对应的英文及整体学习法

    linux命令 注意一下内容收集与互联网,如果觉得有版权问题,请联系. 用Linux命令的时候,如果熟悉对应英文的含义,更有助于理解相应的命令.man: Manual 意思是手册,可以用这个命令查询其 ...