LeetCode题解:(139) Word Break
题目说明
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".
题目分析
这道题的意思是判断字符串能否由字典里的单词组成,一开始只是以为将字符串分成两部分就可以,进行一些样例测试后才发现可以拆成任意数量的单词,只好重写。
个人思路很简单:
- 将字典里的单词一一和字符串进行比对,将符合的索引区间存储在map里,这样就把字典的单词转化成了不同的区间信息;
- 建立长度为字符串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的更多相关文章
- 【LeetCode】139. Word Break 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】139 - Word Break
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- leetcode 139. Word Break 、140. Word Break II
139. Word Break 字符串能否通过划分成词典中的一个或多个单词. 使用动态规划,dp[i]表示当前以第i个位置(在字符串中实际上是i-1)结尾的字符串能否划分成词典中的单词. j表示的是以 ...
- 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 ...
- 【LeetCode】140. Word Break II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归求解 日期 题目地址:https://leetc ...
- [LeetCode] 139. Word Break 单词拆分
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...
- Leetcode#139 Word Break
原题地址 与Word Break II(参见这篇文章)相比,只需要判断是否可行,不需要构造解,简单一些. 依然是动态规划. 代码: bool wordBreak(string s, unordered ...
- 139. Word Break
题目: Given a string s and a dictionary of words dict, determine if s can be segmented into a space-se ...
- 【LeetCode OJ】Word Break II
Problem link: http://oj.leetcode.com/problems/word-break-ii/ This problem is some extension of the w ...
- 【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 ...
随机推荐
- WPF 事件触发命令
方法一使用mvvmlight: xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Int ...
- python基础学习1-第一个网络爬虫程序
#!/usr/bin/env python # -*- coding:utf-8 -*- 煎蛋网抓妹子图 import urllib.request import os import random d ...
- AGC 007 D - Shik and Game
D - Shik and Game 链接 题意: 数轴上有一个人,从0出发到E,速度为1.数轴上还有n只熊,每只熊会在经过后的T时刻后产生一个金币.给定E,T以及n个熊的坐标pi,求收集完所有金币并到 ...
- 原来new的java对象不是所有的都存在堆内存
JVM在晚期(运行期)优化时,使用到一种技术----逃逸分析. 补充说明:逃逸分析在1999年就已提出,但是JDK1.6才实现逃逸分析.而且,这项优化目前仍不成熟,仍有很大改进余地. 这是大坑啊,目前 ...
- .NET:关于数据模型、领域模型和视图模型的一些思考
背景 数据模型.领域模型和视图模型是“模型”的三种角色,一些架构用一种类型表示这三种角色,如:传统三层架构.也有一些架构用两种类型表示这三种角色,如:结合ORM的领域驱动架构.非常少见的场景是用三种类 ...
- VBA_把相同行一列追加数据到一行
Sub Test() Dim rowsNum, i, j, equalRowsNum As Integer rowsNum = ActiveSheet.UsedRange.Rows.Count '获得 ...
- 前端常见算法面试题之 - 重建二叉树[JavaScript解法]
题目描述 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字.例如输入前序遍历序列[1,2,4,7,3,5,6,8],和中序遍历序列[4,7 ...
- Docker容器的启动与停止
启动docker:systemctl start docker 停止docker:systemctl stop docker 重启docker:systemctl restart docker 查看d ...
- Python函数初识二
一.变量的作用域LEGB 1.1.变量的作用域 在Python中,程序的变量并不是在哪个位置都可以访问的,访问权限决定于这个变量是在哪里赋值的.变量的作用域决定了在哪一部分程序可以访问哪个特定的变量名 ...
- Array.Copy 数据是克隆吗?
偶然看到 Array.Copy 方法的时候,想到,它是否是克隆,又是否是深克隆. 做了一个测试 public class abc { public string hello; } [TestMetho ...