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. XML 十六进制值 是无效的字符错误 解决方法之一 转

    /// <summary> /// 过滤非打印字符 /// </summary> /// <param name="tmp">待过滤</p ...

  2. 浅谈Supermap iClient for JavaScript 弹窗类

    地图作为信息的载体和呈现方式,是GIS的重要组成部分,它是一个浏览信息的窗口,在信息日益发达的今天 ,各种地图应用如雨后春笋一般出现在大众眼前,而不是像以往一样太过局限于专业的领域.而弹窗,是作为地图 ...

  3. MySQL查询近一个月的数据

    MySQL查询近一个月的数据 近一个月统计SQL select user_id, user_name, createtime from t_user where DATE_SUB(CURDATE(), ...

  4. Android如何避免输入法弹出时遮挡住按钮或输入框

    在AndroidManifest.xml中为对应的activity添加android:windowSoftInputMode="adjustResize" 在AndroidMani ...

  5. Android新手常见问题(一)

    [1]AAPT2 error: check logs for details File->Settings->Build->Gradle一看path里有中文 最根本的原因是因为use ...

  6. xmanager连接redhat(centos)

    1.以连接Centos6.5为例,先关闭防火墙 service iptables stop chkconfig --level 345 iptables off 2.安装XDM,通过 XDMCP 支持 ...

  7. 深入理解JavaScript系列(49):Function模式(上篇)

    介绍 本篇主要是介绍Function方面使用的一些技巧(上篇),利用Function特性可以编写出很多非常有意思的代码,本篇主要包括:回调模式.配置对象.返回函数.分布程序.柯里化(Currying) ...

  8. 将JavaScript语句插入HTML文档

    (1) 使用 <SCRIPT> 标签将语句嵌入文档 <script type="text/javascript"> function Que() { } & ...

  9. tsung

    要做针对mongodb的压力测试,下了个tsung,看看他的策略是什么,目前定位ts_launcher.erl:do_launch({Intensity, MyHostName, PhaseId})- ...

  10. Redis实现主从复制(Master&Slave)

    由于前段时间公司项目比较赶,一直抽不出时间写博客,今天偷空写一篇吧.前面给大家讲解了单机版redis的基本操作,现在继续给大家讲解一下Redis的进阶部分,主从复制和读写分离. 一.Master&am ...