LeetCode139:Word Break
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words.
For example, given
s = “leetcode”,
dict = [“leet”, “code”].
Return true because “leetcode” can be segmented as “leet code”.
记得最開始做动态规划的题时是打开过这道题的,可是那时没什么思路。如今动态规划的题刷了不少了,今天再做这道题时非常快就想出了状态和状态转移方程。不能不说还是有点进步。
定义A[i]表示0到下标为i的子字符是否能被切割成dict中的多个单词。
那么A[i]与A[j],0<=j< i都有关系,即A[i]与前A[]中的前i-1项都有关系,详细为:
- 假设A[0]为1。推断s中下标从1開始到i结束的字符是否在dict中,假设在,设置A[i]为1,跳出。否则进入第二步。
- 假设A[1]为1,推断s中下标从2開始到i结束的字符是否在dict中。假设在。设置A[i]为1,跳出,否则进入第二步;
…..
这样一直遍历到A[i-1]位置。在上面的遍历过程中假设遍历到某一步j,A[j]=1而且j+1到i表示的字符串出如今dict中,表示前j个字符串能切割成dict中的单词,j+1到i中的字符串串也能切割成dict中的单词。这样表示前i个字符能被切割成dict中的单词。
实际编写代码时,j能够从i開始倒着開始遍历,这样能够降低遍历的次数。
runtime:4ms
class Solution {
public:
bool wordBreak(string s, unordered_set<string>& wordDict) {
int length=s.size();
int *A=new int[length]();
for(int i=0;i<length;i++)
{
for(int j=i;j>=0;j--)
{
if(j==i)
{
A[i]=isExist(s,0,i,wordDict);
}
else if(A[j]==1)
{
A[i]=isExist(s,j+1,i,wordDict);
}
if(A[i]==1)
break;
}
}
return A[length-1]==1;
}
int isExist(string &s,int first,int last,unordered_set<string> &wordDict)
{
string str=s.substr(first,last-first+1);
if(wordDict.count(str))
return 1;
else
return 0;
}
};
LeetCode139:Word Break的更多相关文章
- Leetcode139. Word Break单词拆分
给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,判定 s 是否可以被空格拆分为一个或多个在字典中出现的单词. 说明: 拆分时可以重复使用字典中的单词. 你可以假设字典中没有重复 ...
- [LeetCode] Word Break II 拆分词句之二
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...
- 【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 ...
- 17. Word Break && Word Break II
Word Break Given a string s and a dictionary of words dict, determine if s can be segmented into a s ...
- LeetCode:Word Break II(DP)
题目地址:请戳我 这一题在leetcode前面一道题word break 的基础上用数组保存前驱路径,然后在前驱路径上用DFS可以构造所有解.但是要注意的是动态规划中要去掉前一道题的一些约束条件(具体 ...
- LeetCode Word Break II
原题链接在这里:https://leetcode.com/problems/word-break-ii/ 题目: Given a string s and a dictionary of words ...
- 【LeetCode OJ】Word Break II
Problem link: http://oj.leetcode.com/problems/word-break-ii/ This problem is some extension of the w ...
- Leetcode#139 Word Break
原题地址 与Word Break II(参见这篇文章)相比,只需要判断是否可行,不需要构造解,简单一些. 依然是动态规划. 代码: bool wordBreak(string s, unordered ...
- 【leetcode】Word Break (middle)
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
随机推荐
- mac链接linux终端,shell脚本发布代码
项目的业务需求:从mac端直接连上linux服务终端,并发布相关的代码 一.使用ssh链接上linux服务端 1.cd ~/.ssh 2.vi config,按照下面的内容配置config文件,然后: ...
- phpstudy APACHE支持.htaccess以及 No input file specified解决方案
APACHE支持.htaccess以及 No input file specified解决方案 你的Apache安装文件夹conf里找到httpd.conf文件 索LoadModule rewrite ...
- liunx 系统调用 getopt() 函数
命令行参数解析函数 -- getopt() getopt()函数声明如下: #include <unistd.h>int getopt(int argc, char * const arg ...
- Virtual
Virtual 作用: 允许在派生类中重新定义与基类同名函数并且可以通过其类的指针或引用来访问基类何派生类的同名函数. 1. 概述简单地说,每一个含有虚函数(无论是其本身的,还是继承而来的)的类都至少 ...
- RE : 球体波浪倒计时
背景: 移动端需要做一个倒计时球体水波的效果.主要用到了CSS的SVG瞄点动画和JS的计时器.该动画原型来自于 使用球体水面波动显示进度动画 http://wow.techbrood.com/fid ...
- webStorm恢复误删除文件或工程
背景: 提交git时可能由于没有提交所有文件,不知什么原因导致最近几天所写文件全部消失,所改文件全部恢复到以前.最终通过webstorm找回项目.下面总结通过webstorm找回误删文件,或恢复到历史 ...
- 深入理解java虚拟机_第二章_读书笔记
1.本章内容目录: 概述 运行时数据区域 程序计数器 java虚拟机栈 本地方法栈 java堆 方法区 运行时常量池 直接内存 HotSpot虚拟机对象探秘 对象的创建 对象的内存布局 对象的访问定位 ...
- [转载] Linux的Top命令解析
转载自http://www.jb51.net/LINUXjishu/34604.html.http://blog.csdn.net/hello_yang213/article/details/7455 ...
- 设计模式的征途—21.迭代器(Iterator)模式
我们都用过电视机遥控器,通过它我们可以进行开机.关机.换台.改变音量等操作.我们可以将电视机看做一个存储电视频道的集合对象,通过遥控器可以对电视机中的频道集合进行操作,例如返回上一个频道.跳转到下一个 ...
- C#设计模式之十五命令模式(Command Pattern)【行为型】
一.引言 今天我们开始讲"行为型"设计模式的第二个模式,该模式是[命令模式],又称为行动(Action)模式或交易(Transaction)模式,英文名称是:Command P ...