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

解题思路:

问题: 根据给定的单词字典,判断一个字符串是否可以全部被分割为有效单词。

第一个问题,给定一个字符串, 怎么判断它是不是一个有效单词呢?

本人首先想到是之前做过的 Trie Tree ,打算用 Trie Tree 结构来判断一个字符串是不是一个有效单词。

后来才知道, unordered_set 是 C++ 自带的数据结构,能直接用来检索一个字符串是否为有效单词。囧。看来对 C++ 还不够熟悉。

值得一提的是,用 Trie Tree 实现的算法也通过了 LeetCode 的测试,当然,unordered_set 的完成效率更快。

第二个问题,如何判断一个字符串是否可以全部被分割为有效单词,即原问题。

假设将字符串 s 分割为两段,[0,i-1], [i, n-1],如果[0, i-1] 为有效单词,[i, n-1]为有效单词集合,那么 s 就是一个有效字符串。

将 i 从 1 到 n-1 遍历一次,求得 s 是否是一个有效字符串。

第三个问题,效率满,出现很多反复求解的子问题。

DP思路,即表格法,记录已计算结果。

    vector<int> v;

    bool isMatch(string s, unordered_set<string>& wordDict){

        unordered_set<string>::iterator us_iter = wordDict.find(s);

        if (us_iter != wordDict.end()) {
return true;
} for (int i = ; i < s.size(); i++) {
string leftS = s.substr(, i); unordered_set<string>::iterator leftS_iter = wordDict.find(leftS);
if (leftS_iter != wordDict.end()) { bool isWordR;
if (v[i] != -) {
isWordR = v[i];
}else{
isWordR = isMatch(s.substr(i, s.size() - i), wordDict);
v[i] = isWordR;
} if (isWordR) {
return true;
}
}
} return false;
} bool wordBreak(string s, unordered_set<string>& wordDict) {
vector<int> tmp(s.size(), -); v = tmp;
bool res = isMatch(s, wordDict); return res;
}

[LeetCode] Word Break 解题思路的更多相关文章

  1. [LeetCode] Word Break II 解题思路

    Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...

  2. LeetCode: Word Break II 解题报告

    Word Break II Given a string s and a dictionary of words dict, add spaces in s to construct a senten ...

  3. 【LeetCode】139. Word Break 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  4. [leetcode]Word Break II @ Python

    原题地址:https://oj.leetcode.com/problems/word-break-ii/ 题意: Given a string s and a dictionary of words  ...

  5. [LeetCode] Word Break II 拆分词句之二

    Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...

  6. LeetCode ||& Word Break && Word Break II(转)——动态规划

    一. Given a string s and a dictionary of words dict, determine if s can be segmented into a space-sep ...

  7. LeetCode:Word Break II(DP)

    题目地址:请戳我 这一题在leetcode前面一道题word break 的基础上用数组保存前驱路径,然后在前驱路径上用DFS可以构造所有解.但是要注意的是动态规划中要去掉前一道题的一些约束条件(具体 ...

  8. LeetCode Word Break II

    原题链接在这里:https://leetcode.com/problems/word-break-ii/ 题目: Given a string s and a dictionary of words  ...

  9. [leetcode]Word Break @ Python

    原题地址:https://oj.leetcode.com/problems/word-break/ 题意: Given a string s and a dictionary of words dic ...

随机推荐

  1. Jsoup解析Html教程

    Jsoup应该说是最简单快速的Html解析程序了,完善的API以及与JS类似的操作方式,为Java的Html解析带来极大的方便,结合多线程适合做一些网络数据的抓取,本文从一下几个方面介绍一下,篇幅有限 ...

  2. Windows服务安装方法

    操作系统:Win8.1 安装方法:在命令行窗口中输入:InstallUtil service.exe 出错原因:需要以管理员身份启动命令行.

  3. web开发基础(同步更新中)

    1/Get与Post的区别 GET是我们都熟悉的.它用于请求网页文本.当你在浏览器输入harvard.edu,它会直接访问Harvard的web服务器,去GET /. 第二个最有名的是POST,它经常 ...

  4. java 反射取得方法入参类型的泛形

    package TestReflectClass; import java.util.List; /** * Created by wangyang on 2016/12/16. */ public ...

  5. HDU 1114 Piggy-Bank(完全背包)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1114 题目大意:根据储钱罐的重量,求出里面钱最少有多少.给定储钱罐的初始重量,装硬币后重量,和每个对应 ...

  6. Codevs 1140 Jam的计数法 2006年NOIP全国联赛普及组

    1140 Jam的计数法 2006年NOIP全国联赛普及组 传送门 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description Jam是个喜欢标 ...

  7. C++的函数重载 转

    ——每个现象后面都隐藏一个本质,关键在于我们是否去挖掘 写在前面: 函数重载的重要性不言而明,但是你知道C++中函数重载是如何实现的呢(虽然本文谈的是C++中函数重载的实现,但我想其它语言也是类似的) ...

  8. 24种设计模式--桥梁模式【Bridge Pattern】

    今天我要说说我自己,梦想中的我自己,我身价过亿,有两个大公司,一个是房地产公司,一个是服装制造业,这两个公司都很赚钱,天天帮我在累加财富,其实是什么公司我倒是不关心,我关心的是是不是在赚钱,赚了多少, ...

  9. Angularjs简介

    很久没有系统学习一个新技术了,angularjs将会比较系统的讲解这个技术的语法.应用.次类型的博客将会持续更新,博主也是一个初学者,如果有问题欢迎留言讨论. angularjs简介. angular ...

  10. WPF学习(一)控件的公共属性

    Visiblity控件是否可见:枚举类型:Visible表示可见.Collapsed不可见. IsEnabled:控件是否可用:bool类型. Background:背景色. FontSize:字体大 ...