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)的更多相关文章

  1. 【leetcode】Word Break(python)

    思路是这种.我们从第一个字符開始向后依次找,直到找到一个断句的地方,使得当前获得的子串在dict中,若找到最后都没找到.那么就是False了. 在找到第一个后,接下来找下一个断句处,当然是从第一个断句 ...

  2. 【leetcode】Reverse Integer(middle)☆

    Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 总结:处理整数溢出 ...

  3. 【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 ...

  4. 【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 ...

  5. 【leetcode】Next Permutation(middle)

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  6. 【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 ...

  7. 【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 ...

  8. 【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 ...

  9. 【leetcode】Rotate Image(middle)

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

随机推荐

  1. Tomcat遇到的问题The Tomcat server configuration at ServersTomcat v5.5 Server at localhost-config is missing. Check..

    一.解决方法 删除Servers视图,重新创建一个即可.

  2. 【leetcode】354. Russian Doll Envelopes

    题目描述: You have a number of envelopes with widths and heights given as a pair of integers (w, h). One ...

  3. Codevs 1158 尼克的任务

    1158 尼克的任务 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 尼克每天上班之前都连接上英特网,接收他的上司发来的邮件,这些邮 ...

  4. UVALive 3645 Objective: Berlin(最大流 :时序模型)

    题意:已知n(n <= 150)个城市和m(m <= 5000)个航班,每个航班有出发地.到达地.乘坐人数.起飞时间和降落时间(时间用时和分表示),求从一个指定城市出发,去往另一个指定城市 ...

  5. Ueditor中增加迅雷下载支持

    在项目中有遇到需要在Ueditor中加一个链接,迅雷的开头是thunder 会被默认加上http://   最后的 结果就变成了http://thunder://xxxxx 导致用户点击失败: 其实在 ...

  6. DTCMS规格统一赋值

    admin\article_edit.aspx 已经存在 市场价格 和销售价格统一赋值 //赋值规格市场价格 $("#field_control_market_price").bl ...

  7. 使用VS时点右键卡住—不响应的问题

    以前因为论坛,后来因为工作,发现已经好久没有来百度空间了.也好久没人留言或发表评论了,今天自己更新一下吧. 关于使用VS时点右键卡住或不响应的问题,我是在VS 2008中遇到的,不知道其它的版本有没有 ...

  8. DEV GridControl表格数据源为空在表格中间显示提醒字符

    private static void gv_CustomDrawEmptyForeground(object sender, DevExpress.XtraGrid.Views.Base.Custo ...

  9. Review PHP设计模式之——注册模式

    注册模式: class DbConnections{ var $_store = array(); public function isValid($key) { return isset($this ...

  10. setcookie,getcookie,delcookie,setpostBgPic

    function setCookie(name,value) { var Days = 365; //此 cookie 将被保存 30 天 var exp = new Date(); //new Da ...