leetcode140 Word Break II
思路:
直接爆搜会超时,需要使用记忆化搜索。使用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的更多相关文章
- 【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 ...
- 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 ...
- 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 ...
- 【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 ...
- 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 ...
- [Leetcode Week9]Word Break II
Word Break II 题解 题目来源:https://leetcode.com/problems/word-break-ii/description/ Description Given a n ...
- 【Word Break II】cpp
题目: Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where e ...
- 140. Word Break II(hard)
欢迎fork and star:Nowcoder-Repository-github 140. Word Break II 题目: Given a non-empty string s and a d ...
- leetcode 139. Word Break 、140. Word Break II
139. Word Break 字符串能否通过划分成词典中的一个或多个单词. 使用动态规划,dp[i]表示当前以第i个位置(在字符串中实际上是i-1)结尾的字符串能否划分成词典中的单词. j表示的是以 ...
随机推荐
- mac上Firefox安装firebug和firepath
Firefox安装Selenium ide插件时提示:该附加组件无法安装 因为它似乎已损坏,如下图: 此时千万不要去想着这个插件有问题,可能是Firefox的版本问题. 在Firefox45之后的新版 ...
- 回味经典——uboot1.1.6 之 第二阶段 第三阶段
转自:http://blog.csdn.net/lizuobin2/article/details/52061530 上篇文章说到,再清 BSS 段之后,CPU 跳转到 sdram 里的 start_ ...
- easy_install 和 pip
原文章:http://blog.csdn.net/xsj_blog/article/details/52037609 easy_install 和 pip的介绍: easy_install和pip都是 ...
- Robot Framework基础学习(一)
Robot Framework语法学习: 一.变量的声明.赋值与使用 1.变量标识符:每个变量都可以用 变量标识符 ${变量名} 来表示. 2.变量声明:可以在TestSuite上点右键或者在Edi ...
- HDOJ-2047
阿牛的EOF牛肉串 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total S ...
- Asset Catalog Help (三)---Adding Image Sets
Adding Image Sets Organize versions of your images in image sets, which you can add to an asset cata ...
- C#实现简易ajax调用后台方法
在当前WEB当中,有些人都会抛弃asp.net的服务器控件,转而使用ajax来进行数据的交互和存储. 当我们大量使用ajax的时候,对于新手而言,肯定会创建很多的ashx或aspx页面,通过拼接参数, ...
- [Xcode 实际操作]一、博主领进门-(14)在顶部状态栏显示风火轮以及为应用程序添加应用图标
目录:[Swift]Xcode实际操作 本文将演示在顶部状态栏显示风火轮. 主要用于在执行某个长时间动作时,提示用户耐心等待动作的执行. 在项目导航区,打开视图控制器的代码文件[ViewControl ...
- 阿里云物联网 .NET Core 客户端 | CZGL.AliIoTClient:3. 订阅Topic与响应Topic
文档目录: 说明 1. 连接阿里云物联网 2. IoT 客户端 3. 订阅Topic与响应Topic 4. 设备上报属性 4.1 上报位置信息 5. 设置设备属性 6. 设备事件上报 7. 服务调用 ...
- 笔记-JavaWeb学习之旅11
请求转发:一种在服务器内部的资源跳转方式 使用步骤 1.通过request对象获取请求转发器对象:RequestDispatcher getRequestDispatcher(String path) ...