【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). ...
随机推荐
- JAVA 基础加强学习笔记
一.面向对象 (一)继承 1.继承的好处: (1) 提高了代码的复用性. (2) 让类与类之间产生了关系,提供了另一个特征多态的前提. 注意: 子类中所有的构造函数都会默认访问父类中的空参数的构造函 ...
- 0-1背包问题与N皇后问题的纠结
昨日同学要我帮他看一道算法,如下: 是不是乍一看是“0-1背包”问题呀,我也这么想,于是就这么兴致勃勃的开始用这个想法去思考怎么算.但是算法也忘得差不多,回去赶紧补补,也趁着这次机会好好复习一下算法, ...
- 关于C#正则表达式MatchCollection类的总结,正则表达式的应用
认识MatchCollection 类 表示通过以迭代方式将正则表达式模式应用于输入字符串所找到的成功匹配的集合. 命名空间: System.Text.RegularExpressions 属性:C ...
- 统计某一字段等于不同值的个数的sql语句(分享)
本文介绍下,用一条sql语句统计某一字段等于不同值的个数,方法很独特,有需要的朋友参考下. 表t,数据: id type001 1001 0002 1001 ...
- Hibernate一对一双向关联映射
关键原因在于对象模型具有方向性: 单向:一端只能加载另一端,不能反过来. 双向:两端都可以加载另一端. 问题来了:如何我们想从身份证端(IdCard)加载人(Person),怎么办呢? 下面我们开始介 ...
- Python试卷
3.写一个函数,计算一个给定的日期是该年的第几天. def getday(self,y=None,m=None,d=None): date = datetime(y,m,d) days = date. ...
- oracle 行转列 分析函数
oracle 行转列 首先看一下源数据: 方法一:WM_CONCAT group by 这个方法没有问题. SELECT CODE_TS, WMSYS.WM_CONCAT(S_NUM + || ':' ...
- oracle查询和设置过期时间
第一步:找到oracle 打开enterprise Manager Console如下图: 第二步,找到概要文件: sys 用户进入,找到你的数据库(如:ora8)-“安全性”-"用户&qu ...
- iOS 基础 第三天(0807)
0807 成员变量作用域###### 如下图所示: 这里要注意手写的成员变量/实例变量默认的作用域是private,所以外部指针类型的对象无法直接访问,这起到一定的保护作用.但可以在当前类内部@imp ...
- Linux 配置Samba
配置Samba 如果没有普通用户,添加用户,指令: useradd -m aaa passwd bbb 添加Samba用户指令: smbpasswd -a aaa passwdword: …… 修改配 ...