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

解题思路:

可以使用动态规划的思想,如果S[0~i]在dict中是可分的,那么只需考察S[i+1~n]在dict中可分即可;

具体步骤:

新建一个和S字符串长度相等的数组,用来记录S从0~i的各个子串是否被dict可分;

从S串的第一个字符开始,逐渐增加字符,考察是否可分:

对于待考察的S[0~i]字符串,如果S[0~j]已经在之前的考察中证实是可分的,那么只需判断S[j+1~i]是否可分;

代码:

 class Solution {
public:
bool wordBreak(string s, unordered_set<string>& wordDict) {
vector<bool> seg_flag(s.size(), false); for (int i = ; i < s.size(); ++i) {
if (wordDict.find(s.substr(, i+)) != wordDict.end()) {
seg_flag[i] = true;
continue;
} for (int j = i; j >= ; --j) {
if (seg_flag[j-] == true && (wordDict.find(s.substr(j, i - j + )) != wordDict.end())) {
seg_flag[i] = true;
break;
}
}
} return seg_flag[s.size() - ];
}
};

【Leetcode】【Medium】Word Break的更多相关文章

  1. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  2. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  3. 【LeetCode题意分析&解答】35. Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  4. ACM金牌选手整理的【LeetCode刷题顺序】

    算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...

  5. 【leetcode刷题笔记】Word Ladder II

    Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...

  6. 【leetcode刷题笔记】Length of Last Word

    Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...

  7. 【LeetCode算法-58/66】Length of Last Word/Plus One

    LeetCode第58题: Given a string s consists of upper/lower-case alphabets and empty space characters ' ' ...

  8. 【LeetCode每天一题】Word Search(搜索单词)

    Given a 2D board and a word, find if the word exists in the grid.The word can be constructed from le ...

  9. 【LeetCode每天一题】Length of Last Word(字符串中最后一个单词的长度)

    Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...

  10. 【leetcode刷题笔记】Word Search

    Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...

随机推荐

  1. python 使用缓存加快运算

    from functools import lru_cache import time from functools import wraps def clock(func): @wraps(func ...

  2. 转: 将Eclipse代码导入到AndroidStudio的两种方式 ,测试了方法2,成功。

    蛋疼,不知道为什么我的eclipse的logcat总是莫名其妙的显示一堆黄色字体的字,看不懂的那种,如下图: 然后查了一下资料,说可能是adt版本太低,手机系统太高. 然后本来想升级adt,但是各种折 ...

  3. maven多层项目配置

    今天遇到一个maven项目有3个子项目的配置问题,一开始项目结构是混乱的,而且包引入不能正常解析. 主项目上右键,选择configure->configure and detect nested ...

  4. IE11在使用get方式提交没有进行请求的bug问题

    在做iemsc项目的时候,测试提交了一个bug问题,在发布新闻成功后,自动刷新列表的时候,不进行刷新,但是在谷歌上面又不会出现这种问题, 原因: 发现请求的时候用的get请求,因为不同的浏览器的请求机 ...

  5. Android ListView分组显示

    ListView的实现方法也是普通的实现方法.只不过在list列表中加入groupkey信息.在渲染的时候要判断是否是分组的标题. 就是在使用不同的两个View的时候存在这种情况,convertVie ...

  6. Orcale 之 SQL 数据定义

    SQL 的数据定义功能主要是针对数据对象进行定义的,这些数据对象主要包括:表,视图以及索引. 注意:由于视图是基于表的虚表,而索引是依附在基表上的,所以视图和索引均不提供修改视图和索引定义的操作.如果 ...

  7. 通过反射获取及调用方法(Method)

    1.获取方法使用反射获取某一个类中的方法,步骤:①找到获取方法所在类的字节码对象②找到需要被获取的方法 Class类中常用方法: public Method[] getMethods():获取包括自身 ...

  8. Java - Latch和Barrier的区别

    之所以把Latch与Barrier放在一起比较是因为他们给人一种相似的感觉. 他们都是阻塞一些行为直至某个事件发生,但Latch是等待某个事件发生,而Barrier是等待线程. 先比较一下JCIP中对 ...

  9. [转]字符编码笔记:ASCII,Unicode 和 UTF-8

      本文非原创,转载 ,原文地址 :http://www.ruanyifeng.com/blog/2007/10/ascii_unicode_and_utf-8.html 作者: 阮一峰 日期: 20 ...

  10. 如何移除git不需要提交的文件

    在大公司提交代码都需要经历cr(code review)过程,在用python脚本将代码上传至cr(代码对比工具)服务器时会产生一个issue.info文件,这个文件的内容就是一个issue号,此文件 ...