思路:

直接爆搜会超时,需要使用记忆化搜索。使用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. [YNOI 2016] 掉进兔子洞

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=4939 [算法] 不难发现 , ansi = (r1 - l1 + 1) + (r2 ...

  2. ubuntu 16.04 apt-get 出现The package 'xxx' needs to be reinstalled, but I can't find an archive for it.

    参考网址:http://www.ihaveapc.com/2011/10/fix-annoying-the-package-needs-to-be-reinstalled-but-i-cant-fin ...

  3. 11.boolean类型

    /* 关于java中的Boolean类型: 1)赋给boolean类型的值只有两个:ture或者false 2)boolean类型的数据主要用在逻辑运算和条件控制语句中.*/ public class ...

  4. npm安装cnpm淘宝镜像

      npm set registry https://registry.npm.taobao.org # 注册模块镜像 npm set disturl https://npm.taobao.org/d ...

  5. docker容器磁盘

    docker容器磁盘扩容 一.配置文件里更改容器创建时的默认磁盘大小 [root@ip---- ~]# cat /etc/sysconfig/docker-storage DOCKER_STORAGE ...

  6. SQL 系统表

    http://www.cnblogs.com/asdcer/archive/2007/05/14/746377.aspx

  7. Ubuntu安装vnc+gnome的xstartup配置

    Log 1 安装vncserver并且在xstartup配置gnome 背景:学习Ruby,想在ubuntu下使用rubymine 时间:2014-3-10 环境:Ubuntu 记录:Roy 其实这个 ...

  8. 浅谈SpringBoot核心注解原理

    SpringBoot核心注解原理 今天跟大家来探讨下SpringBoot的核心注解@SpringBootApplication以及run方法,理解下springBoot为什么不需要XML,达到零配置 ...

  9. 动态加载dll

    extern "C" MMUPDATENOTIFY_IMPEXP bool _cdecl NotifyThrift(char* chThriftIp, char* chPor) H ...

  10. Unity5.0 EventSystem事件系统的详细说明

    http://www.manew.com/blog-56596-2917.html?_dsign=53b25d5d unity5.0开发了新的UI系统UGUI,附带的事件系统EventSystem不仅 ...