思路:

直接爆搜会超时,需要使用记忆化搜索。使用map把已经计算过的情况记录下来,避免重复计算。

实现:

 class Solution
{
public:
vector<string> wordBreak(string s, vector<string>& wordDict)
{
unordered_map<int, vector<string>> dp;
return dfs(s, , wordDict, dp);
}
vector<string> dfs(string s, int cur, vector<string>& wordDict, unordered_map<int, vector<string>>& dp)
{
if (cur >= s.length())
{
vector<string> v;
v.push_back("");
return v;
}
if (dp.count(cur)) return dp[cur];
vector<string> ans;
for (auto it: wordDict)
{
int l = it.length();
if (cur + l <= s.length() && s.substr(cur, l) == it)
{
if (cur + l == s.length())
ans.push_back(it);
else
{
vector<string> tmp = dfs(s, cur + l, wordDict, dp);
for (auto it1: tmp)
{
ans.push_back(it + " " + it1);
}
}
}
}
return dp[cur] = ans;
}
};

leetcode140 Word Break II的更多相关文章

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

  2. 17. Word Break && Word Break II

    Word Break Given a string s and a dictionary of words dict, determine if s can be segmented into a s ...

  3. LeetCode之“动态规划”:Word Break && Word Break II

     1. Word Break 题目链接 题目要求: Given a string s and a dictionary of words dict, determine if s can be seg ...

  4. 【LeetCode】140. Word Break II

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

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

  6. [Leetcode Week9]Word Break II

    Word Break II 题解 题目来源:https://leetcode.com/problems/word-break-ii/description/ Description Given a n ...

  7. 【Word Break II】cpp

    题目: Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where e ...

  8. 140. Word Break II(hard)

    欢迎fork and star:Nowcoder-Repository-github 140. Word Break II 题目: Given a non-empty string s and a d ...

  9. leetcode 139. Word Break 、140. Word Break II

    139. Word Break 字符串能否通过划分成词典中的一个或多个单词. 使用动态规划,dp[i]表示当前以第i个位置(在字符串中实际上是i-1)结尾的字符串能否划分成词典中的单词. j表示的是以 ...

随机推荐

  1. 【EOJ Monthly 2018.2 (Good bye 2017)】

    23333333333333333 由于情人节要回家,所以就先只放代码了. 此题是与我胖虎过不去. [E. 出老千的 xjj] #include<cstdio> #include<c ...

  2. Java笔记(五)

    泛型:JDK1.5版本后出现的新特性.用于解决安全问题,是一个类型安全机制. 好处:将运行期间出现问题ClassCastException,转移到了编译时期.方便程序员解决问题,让运行时问题减少. 避 ...

  3. 「LuoguP3808」 【模板】AC自动机(简单版)

    题目背景 通过套取数据而直接“打表”过题者,是作弊行为,发现即棕名. 这是一道简单的AC自动机模板题. 用于检测正确性以及算法常数. 为了防止卡OJ,在保证正确的基础上只有两组数据,请不要恶意提交. ...

  4. 图片轮播和C3动画

    值得注意的地方是:如果在图片下面的li做背景图片,可以把li上的数字或者文本设置为transparent(透明色),这样的话既可以实现轮播的定时器效果,也可以让li上有C3动画效果.如果li上的文字不 ...

  5. UNP总结 Chapter 12~14 IPv4与IPv6的互操作性、守护进程和inet超级服务器、高级I/O函数

    一.IPv4与IPv6的互操作性 1.IPv4客户与IPv6服务器 拥有双重协议栈的主机的一个基本特性就是:其上运行的IPv6服务器既能应付IPv4客户,又能应付IPv6客户.这是通过使用IPv4映射 ...

  6. APACHE2 服务器配置 (一)

    1.安装 sudo apt-get install apache2 2.重启: sudo service apache2 resatrt 3.设置根目录: /var/www 设置方法: 2.2版: / ...

  7. 《Kubernetes权威指南第2版》学习(一) Kubernetes是什么

    1.1 Kubernetes是什么? 首先,它是一个全新的基于容器技术的分布式架构领先方案.是谷歌的Borg(大规模集群管理系统)的一个开源版本. 其次,如果系统设计遵循了Kubernetes的设计思 ...

  8. 【旧文章搬运】深入分析Win7的对象引用跟踪机制

    原文发表于百度空间及看雪论坛,2010-09-12 看雪论坛地址:https://bbs.pediy.com/thread-120296.htm============================ ...

  9. 一、mysql简述

    该套讲义参考动力节点郭鑫老师的mysql视频整理所得 1.DBMS--数据库管理系统  Data Base Management System eg: mysql数据库管理系统 2.DB--数据库/仓 ...

  10. struts2-045漏洞浅析

    http://blog.csdn.net/rossrocket/article/details/67674290