【leetcode】Word Break (middle)
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"
.
思路:
这道题最开始因为unordered_set纠结了好久。后来突然不知怎么的就顿悟了,也不记得之前到底在纠结什么了...
因为做了Word Break II,这道题就照猫画虎的做了出来。开始我存储了单词到达每个位置时的起始位置,后来发现不用,只要记录能不能到达当前位置即可。
bool wordBreak(string s, unordered_set<string> &dict) {
if(dict.empty() || s.empty())
return false;
vector<bool> v(s.length() + , false); //不需要存储是哪些位置开始的单词到达了v[j] 只要记录到没到就可以了
v[] = true;
for(int i = ; i <= s.length(); i++) //每一个单词的结束的下一个位置
{
for(int j = ; j < i; j++)//每个单词开始的位置
{
if(v[j] && dict.find(s.substr(j, i - j)) != dict.end()) //必须可以到达j 并且可以在字典中找到子串才置为真
{
v[i] = true;
break; //一旦为真就跳出本层判断 循环下一个结束位置
}
}
}
return v[s.length()];
}
【leetcode】Word Break (middle)的更多相关文章
- 【leetcode】Word Break(python)
思路是这种.我们从第一个字符開始向后依次找,直到找到一个断句的地方,使得当前获得的子串在dict中,若找到最后都没找到.那么就是False了. 在找到第一个后,接下来找下一个断句处,当然是从第一个断句 ...
- 【leetcode】Reverse Integer(middle)☆
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 总结:处理整数溢出 ...
- 【leetcode】Reorder List (middle)
Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do thi ...
- 【leetcode】Spiral Matrix(middle)
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- 【leetcode】Next Permutation(middle)
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- 【leetcode】Course Schedule(middle)☆
There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...
- 【leetcode】Rotate List(middle)
Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...
- 【leetcode】Partition List(middle)
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
- 【leetcode】Rotate Image(middle)
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
随机推荐
- DOS批处理命令-pause
“pause”用于暂停批处理的执行器并给出提示信息,然后由用户决定是继续执行还是终端执行. 语法: pause 简单一个pause,不附带任何参数. 执行pause以后会提示一句话[按任意键继续执行] ...
- iOS开发——消息推送跳转
项目开发用集成是极光推送JPush 这里主要是消息推送过来处理对应界面跳转 同时看到两篇写的不错的相关博客分享一下: http://www.jianshu.com/ ...
- 控制GridView中字段的长度,规范数据
前台: <asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridVi ...
- .Net Core 中的包、元包与框架(Packages, Metapackages and Frameworks)
包,元包与框架 本文翻译自 Packages, Metapackages and Frameworks. .Net Core 是一种由 NuGet 包组成的平台.一些产品体验受益于代码包的细粒度定义, ...
- windows下tomcat切割日志按照日期输出
windows下tomcat默认不会把应用的日志信息输出在日志文件中的,只会在控制台打印. 解决方法: 1,下载工具cronolog-1.6.1-win32.zip,并解压. 2,把cronolog. ...
- 单引号,双引号 和 heredoc 初始化php字符串之间的区别
php中的字符串指的是字符的序列,可以通过三种方式初始化:单引号.双引号和使用here document(heredoc) 形式. 一:单引号时 ①:需要转义的特殊字符:反斜杠和单引号. ②:因为ph ...
- setTimeout/setInterval执行时机
setTimeout()和setInterval()经常被用来处理延时和定时任务.setTimeout() 方法用于在指定的毫秒数后调用函数或计算表达式,而setInterval()则可以在每隔指定的 ...
- [转]Excel生成批量SQL语句,处理大量数据的好办法
当有大量重复体力工作写入或修改数据到数据库中时,可以 第一,将Excel数据整理好了之后,通过SQL的导入功能直接导进数据库,但是得保证数据库字段和Excel的字段一致. 第二,通过Excel来生成对 ...
- 指针之 *((volatile unsigned long *)(x))解析
今天重新温习了一下C语言的指针部分,突然想到了经常会碰见的一种宏定义:#define PGAS (*((volatile unsinged long *)(x))) 在解析该宏定义前,先看看指针变量的 ...
- 数据库降级-从sqlserver 2008 降到 2005
前天遇到一个问题,是一个数据库是Sqlserver 2008的,而平台数据库库是2005的,需要把2008的数据库附加进来,试了很多办法,现在觉得最好的办法就是导出导入办法. 第一步 新建一个Sqls ...