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 ...
随机推荐
- zip 安装mysql 和遇到的坑
在官网下载了mysql 社区版的,官方网址:https://dev.mysql.com/downloads/mysql/ 解压后发现里面没有安装快捷方式,才知道是zip解压,dos窗口安装.这就比界面 ...
- 浅谈字体小图标font awesome,iconfont,svg各自优缺点
三种都是矢量图(即放大不失真),但是个自又有个自的优缺点, 1.font awesome: 优点:相对比较简单,查看官网看例子基本上都会用 (http://www.bootcss.com/p/font ...
- js 切换全屏
公司有一个需求就是点击某一个按钮实现全屏切换功能,然后呢我就在网上扒了段代码.封装了一下.使用的小伙伴们可以看看哦! 切换全屏代码 <!DOCTYPE html> <html> ...
- Python - SIP参考指南 - 介绍
介绍 本文是SIP4.18的参考指南.SIP是一种Python工具,用于自动生成Python与C.C++库的绑定.SIP最初是在1998年用PyQt开发的,用于Python与Qt GUI toolki ...
- android wear开发之:增加可穿戴设备功能到通知中 - Adding Wearable Features to Notifications
注:本文内容来自:https://developer.android.com/training/wearables/notifications/index.html 翻译水平有限,如有疏漏,欢迎批评指 ...
- SDRAM操作(FPGA实现)
对SDRAM基本概念的介绍以及芯片手册说明,请参考上一篇文章SDRAM操作说明. 1. 说明 如图所示为状态机的简化图示,过程大概可以描述为:SDRAM(IS42S16320D)上电初始化完成后,进入 ...
- PHP设计模式之组合模式
当我们的一个对象可能代表一个单一的实体,或者一个组合的实体,但是仍然需要通过同样的方式被使用时,这种情形则适合使用组合模式的设计. 组合模式是一种结构型模式. 当看了书上的解释之后,并不是很理解,遂去 ...
- 裴波那契查找详解 - Python实现
裴波那契查找(Fibonacci Search)是利用黄金分割原理实现的查找方法. 斐波那契查找的核心是: 1.当key == a[mid]时,查找成功: 2.当key < a[mid]时,新的 ...
- Unity3D游戏xlua轻量级热修复框架
这是什么东西 前阵子刚刚集成xlua到项目,目的只有一个:对线上游戏C#逻辑有Bug的地方执行修复,通过考察了xlua和tolua,最终选择了xlua,原因如下: 1)项目已经到了后期,线上版本迭代了 ...
- Pyqt5学习系列
最近在学习Pyqt5做界面,找到了一个非常棒的博主的学习系列 在此记录下来: http://blog.csdn.net/zhulove86/article/category/6381941