思路:

直接爆搜会超时,需要使用记忆化搜索。使用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. JavaScript DOM 编程艺术 ---> JavaScript语法

    二.  JavaScript语法目录 2.1 语法 javaScript代码要通过HTML/XHTML文档才能执行.可以有两种方式完成这一点,第一种是将JavaScript代码放到文档<head ...

  2. spark运行模式之二:Spark的Standalone模式安装部署

    Spark运行模式 Spark 有很多种模式,最简单就是单机本地模式,还有单机伪分布式模式,复杂的则运行在集群中,目前能很好的运行在 Yarn和 Mesos 中,当然 Spark 还有自带的 Stan ...

  3. win7 第一次装 mysql-5.7.16-winx64 ,不知道root 密码,该如何处理?

    转载请注明出处:http://blog.csdn.net/qq_26093511/article/details/52851811 ERROR 1045 (28000): Access denied ...

  4. 用Merge存储引擎中间件实现MySQL分表

    觉得一个用Merge存储引擎中间件来实现MySQL分表的方法不错. 可以看下这个博客写的很清楚--> http://www.cnblogs.com/xbq8080/p/6628034.html ...

  5. 使用BFG清除git仓库中的隐私文件或大文件

    使用git时间不长,在调机械臂项目的时候,由于对TwinCAT3和vs的机制不太了解,没有添加很好的忽略文件(.gitignore).造成git仓库包含了很多没有用的文件,例如vs的sdf文件,Twi ...

  6. Hadoop Shell 介绍

    以 hadoop 2.7.3 为例 bin 目录下是最基础的集群管理脚本, 用户可通过该脚本完成各种功能, 如 HDFS 管理, MapReduce 作业管理等. 作为入门, 先介绍bin 目录下的 ...

  7. dockerfile:制作tomcat镜像+javaweb

    FROM ubuntu:14.10 MAINTAINER linx #把java与tomcat添加到容器中,Add会解压 ADD jdk-8u151-linux-x64.tar.gz /usr/loc ...

  8. springMVC拦截配置

    1.web.xml文件配置 <!-- spring mvc --> <servlet> <servlet-name>DispatcherServlet</se ...

  9. idea ultmate版安装后toolWindows没有database

    点击Configure--->plugins将database勾选

  10. Windows下SVN的下载、安装

    SVN全称Subversion,是一款优秀的版本控制工具. SVN分为服务器.客户端两部分,服务器和客户端版本很多,有的是收费的,有的需要注册.我们选择免费.不用注册的. 以VisualSVN作为服务 ...