leetcode 140 单词拆分2 word break II
单词拆分2,递归+dp,
需要使用递归,同时使用记忆化搜索保存下来结果,c++代码如下
class Solution {
public:
//定义一个子串和子串拆分(如果有的话)的映射
unordered_map<string,vector<string>>m;
vector<string> wordBreak(string s, vector<string>& wordDict) {
if(m.count(s)) return m[s];//当映射的子串有s说明已经判断完了返回拆分m[s]
if(s.empty()) return {""};//假如s是空的返回空串
vector<string> res;
for(string word: wordDict){
if(s.substr(,word.size())!=word) continue;//在s开头找到字典中的词;
vector<string> rem=wordBreak(s.substr(word.size()),wordDict);//rem是剩下的子串拆分,递归调用wordBreak
for(string str:rem){
res.push_back(word+(str.empty() ? "" : " ")+str);//str非空时,前面加个空格后面再加单词;
}
}
return m[s]=res;
}
};
参考:http://www.cnblogs.com/grandyang/p/4576240.html
leetcode 140 单词拆分2 word break II的更多相关文章
- leetcode 139 单词拆分(word break)
一开始的错误答案与错误思路,幻想直接遍历得出答案: class Solution { public: bool wordBreak(string s, vector<string>& ...
- Java实现 LeetCode 140 单词拆分 II(二)
140. 单词拆分 II 给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,在字符串中增加空格来构建一个句子,使得句子中所有的单词都在词典中.返回所有这些可能的句子. 说明: 分 ...
- [LeetCode] 140. 单词拆分 II
题目链接 : https://leetcode-cn.com/problems/word-break-ii/ 题目描述: 给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,在字符 ...
- Java实现 LeetCode 140 单词拆分II
class Solution { public List<String> wordBreak(String s, List<String> wordDict) { List&l ...
- 单词拆分 I · Word Break
[抄题]: 给出一个字符串s和一个词典,判断字符串s是否可以被空格切分成一个或多个出现在字典中的单词. s = "lintcode" dict = ["lint" ...
- LeetCode 139. 单词拆分(Word Break)
139. 单词拆分 139. Word Break
- leetcode 139. Word Break 、140. Word Break II
139. Word Break 字符串能否通过划分成词典中的一个或多个单词. 使用动态规划,dp[i]表示当前以第i个位置(在字符串中实际上是i-1)结尾的字符串能否划分成词典中的单词. j表示的是以 ...
- 【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 ...
随机推荐
- Python内置函数清单
作者:Vamei 出处:http://www.cnblogs.com/vamei Python内置(built-in)函数随着python解释器的运行而创建.在Python的程序中,你可以随时调用这些 ...
- python视频学习笔记4(函数)
函数中return和print的区别,没有return会默认返回None值 函数定义:所谓**函数**,就是把 **具有独立功能的代码块** 组织为一个小模块,在需要的时候 **调用** 1.函数的步 ...
- CentOS 7系统时间与实际时间差8个小时
1.查看系统时间: [root@localhost sysconfig]# timedatectl Local time: 一 2017-11-06 21:13:19 CST Universal ti ...
- python 读excel表操作
import xlrd # 打开文件 data = xlrd.open_workbook('测试表.xlsx') # 查看工作表 data.sheet_names() print("shee ...
- 虚拟机Linux下扩展硬盘的方法
[原文链接]:http://blog.csdn.net/tianlesoftware/article/details/5642883 装虚拟机时空间划小了,于是又加了5G的空间,折腾了半天,挂上去了. ...
- MVC 事物
前一阵学习mvc但是对于关联表的数据操作总是分裂开来写,这样有很大的侥幸,比如嵌套了3个 if(){ if(){ if(){ } } } 如果前两个都是true那么最后一个是false了,这样是不想看 ...
- 织梦网站dedecms防止挂马的思路
DedeCms做为国内使用最为广泛使用人数最多的CMS之一,经常爆出漏洞,每个漏洞的爆出,影响都是一大片,轻则被人挂广告.弹框,重则服务器成为肉机,宝贵数据丢失.那么有什么办法可以提高DedeCms的 ...
- 解决java编译错误:编码 GBK 的不可映射字符 (0x8C)
1. 问题概述: 程序很简单,打印一行字:你好,世界 (使用的工具是:win10自带的记事本.java的jdk:java development kit) 但是在打开终端进行编译时,报出了一个错误:编 ...
- 代码审计-DVWA-命令注入
首先说明,我水平不高,这是我在学习代码审计过程中写的记录笔记,难免有不正之处,还望指出. Windows 10 php7.2.10 + apache DVWA代码审计 命令执行 low <?ph ...
- 用Python实现九九乘法表打印
#!usr/bin/env python # -*- coding:utf-8 -*- # dic={ # 'apple':10, # 'iphon':5000, # 'wwatch Tv':3000 ...