题目说明

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. mybatis第二天——动态SQL与关联查询

    大纲摘要: 1.输入映射和输出映射 a) 输入参数映射 b) 返回值映射 2.动态sql a) If b) Where c) Foreach d) Sql片段 3.关联查询 a) 一对一关联 b) 一 ...

  2. 20155211 Exp1 PC平台逆向破解(5)M

    20155211 Exp1 PC平台逆向破解(5)M 实践目标 本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何用户输入 ...

  3. 自己写个activex控件,如何知道他的classid(转载)

    在网页里用的时候需要知道他的classid我在代码中看到有 const GUID CDECL BASED_CODE _tlid = { 0x89201950, 0x2CAC, 0x4CF7, { 0x ...

  4. Java线程和多线程(十四)——Synchronized关键字解析

    曾经有一个比较有趣的面试问题,那就是,关于使用synchronized关键字,是用在方法上面尾号,还是用在一个代码块上面为好? 答案就是使用锁定代码块为更好.因为这样不会锁定对象.当synchroni ...

  5. P3900 [湖南集训]图样图森破

    P3900 [湖南集训]图样图森破 链接 分析: 感觉像个暴力. 可以枚举回文串的回文中心,即枚举一个串,枚举一个串的位置作为回文中心,然后求出这个串内的回文串的长度. 此时如果回文串两端都没有到这个 ...

  6. 非局部均值滤波算法的python实现

    如题,比opencv自带的实现效果好 #coding:utf8 import cv2 import numpy as np def psnr(A, B): return 10*np.log(255*2 ...

  7. 由 Session 和 Cookie 的区别说起

    Session 和 Cookie 有什么区别? 最近面试被问到这个问题,和面试官一番讨论了解到面试官心里的答案后,我不太满意. 面对上面的问题,如果是刚毕业时的我,一定会毫不犹豫说出 Cookie 是 ...

  8. Python distribute到底使用package_data还是MANIFEST.in?

    今天看Flask的文档,里面提到如何通过distribute发布你自己的Python包.讲包含文件的时候,提到要用MANIFEST.in并将include_package_data设置为True. 由 ...

  9. stringObject.substring(start,stop)

    用于提取字符串中 介于两个指定下标之间的字符. start 必需.一个非负的整数 stop 可选.一个非负的整数

  10. vi/vim 常用命令 之 一图定天下!

    直接上干活,一张图解决~