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. PHP学习笔记 - 进阶篇(6)

    PHP学习笔记- 进阶篇(6) 会话控制(session与cookie) 当前的Cookie为: cookie简介 Cookie是存储在客户端浏览器中的数据,我们通过Cookie来跟踪与存储用户数据. ...

  2. 基于AspectJ自定义注解

    package com.aspectj.demo.aspect; import java.lang.annotation.ElementType; import java.lang.annotatio ...

  3. FAILURE: Build failed with an exception. Crunching Cruncher screen.png failed

    自己测试ionic的模板项目cutePuppyPics时,按照https://github.com/driftyco/ionic/blob/2.0/CHANGELOG.md#angular-updat ...

  4. 产品经理常用工具Axure、Visio、Mindmanager使用解析(摘)

    如果想表现产品的业务流程,那么我建议使用Visio来绘制流程图.如果想表现产品的页面图文布局和页面的跳转关系,我建议使用axure.如果想表现产品的信息架构,我建议使用Mindmanager或Xmin ...

  5. myeclipse 的 working set

    想必大家的myeclipse会有很多工程看的和不方便,那么怎么让它看的简洁一点呢,使用working set 会让你的目录看起来没有那么的多. 首先是怎么创建 working set  ,在新建时选择 ...

  6. 在Apache中开启虚拟主机

    最近在自学LAMP,在Apache中尝试着开启虚拟主机的时候,遇到了挺多麻烦的,这里也顺便总结一下,在Apache中开启虚拟主机的时候,主要有下面几个步骤: 1.新建一个文件夹作为虚拟主机,用来存储网 ...

  7. TFS遇到TF14446错误的解决方法

    先上图 使用TFS,之前遇到文件被删除直接获取最新版本就行了,今天遇到这个异常:[TF14446: 无法签出“$/****/****/**/Models.pdb”以进行编辑.您的客户端或团队项目配置为 ...

  8. swing画太极图案源码

    package org.open.swing.taiji; /** * @(#)Taichi.java * * * @author * @version 1.00 2007/6/12 */ impor ...

  9. NoSQL专家王涛访谈:为什么我们还要做一个NoSQL?

    ChinaUnix:各位网友大家好,今天有幸请到王涛先生到CU做客,与大家交流一些工作经验.首先请王涛先介绍一下自己. 王涛:大家好,我是王涛.过去八年里我一直在IBM多伦多实验室从事DB2引擎研发的 ...

  10. PHP学习之环境搭建

    计算机环境 win7  64位 搭建  apache-httpd-2.2-win64  +  php-5.3.6-Win32-VC9-x64  +MySQL_5.5.13_winx64开发环境 参考: ...