leetcode题目:Word Break和Word Break II

11-4 AEB 算法原理及系统组成 本专题旨在探讨如何利用仿真软件开发自动紧急制动系统AEB,主要通过整理开发流程,实现从需求到验证的全流程掌握,为后续开发提供支持;同时,研究了如何在汽车自动驾驶仿真工具中开发自己的算法,并解决了从ISO、E-CNAP、GB/T以及交通法规等提炼出功能需求的问题,以快速实现原型模型的开发。本专题重点讲解了AEB的工作原理、系统/软件架构和需求、算法模型及实现、CCRs/CCRb/CCRm建模与仿真、VRU-AEB-B/P/M建模与仿真,同时还提供了测试用例,期待将AEB算法成功地产品化。 阅读详情

题目一:

Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words.

For example, given
s = "leetcode",
dict = ["leet", "code"].

Return true because "leetcode" can be segmented as "leet code".

题目二:

Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word.

Return all such possible sentences.

For example, given
s = "catsanddog",
dict = ["cat", "cats", "and", "sand", "dog"].

A solution is ["cats and dog", "cat sand dog"].

两题是类似的,只是第二题需要输出隔断字符串。

分析:

几点题目要求需要领会:

1)存储单词集合的set是无顺序的,所以不能从set中开始挨个寻找单词在组合词是否存在。

2)set中的词可以在组合词中重复出现。

本题是从组合词中挨个单词进行寻找:

代码如下:

class Solution {
public:
    bool wordBreak(string s, unordered_set<string> &dict) 
    {
        int len = s.size();
		vector<bool>m_vec(len+1,false);
		m_vec[0] = true;
		int i = 0;
		int j = 0;
		for (i = 1;i < len+1;i++)
		{
			for (j = i-1;j>=0;j--)
			{
				if (m_vec[j] && dict.find(s.substr(j,i-j))!=dict.end())
				{
					m_vec[i] = true;
					break;
				}
			}
		}
		return m_vec[i-1];
    }
};
题目二:

思路:

1)找出组合词中,存在单词的起始位置,用一个二维数组表示,比如prev[i][j]表示从j到i中的字符串是个单词。

2)在用循环递归的方式插入寻找出答案。

3)在循环递归中,注意清理先前存入的数据。

代码如下:

class Solution {
public:
    vector<string> wordBreak(string s, unordered_set<string> &dict) {
       int len = s.size();
		int i = 0;
		int j = 0;
		vector<bool>m_vec(len+1,false);
		m_vec[0] = true;
		vector<vector<bool>>m_prevec(len+1,vector<bool>(len));
		for(i=1;i<len+1;i++)
		{
			for(j=i-1;j>=0;j--)
			{
				if(m_vec[j]&&dict.find(s.substr(j,i-j))!=dict.end())
				{
					m_vec[i]=true;
					m_prevec[i][j]=true;
				}
			}
		}
		vector<string>path;
		gen_path(s,m_prevec,len,path);
		return m_strvec;
	}
private:
	vector<string>m_strvec;
	void gen_path(string &s,const vector<vector<bool>>&prev,int len,vector<string>&path)
	{
		if (len == 0)
		{
			string str;
			for (vector<string>::reverse_iterator pos = path.rbegin();pos!=path.rend();pos++)
			{
				str.append(*pos);
				str.append(" ");
			}
			str.erase(--str.end());
			m_strvec.push_back(str);	
		}
		for (int i=0;i<s.size();i++)
		{
			if (prev[len][i])
			{
				path.push_back(s.substr(i,len-i));
				gen_path(s,prev,i,path);
				path.pop_back();
			}
		}		
	}
};



ABAP 7.40+新语法实战:用VALUE和REDUCE五分钟搞定复杂内表统计 本文详细介绍了ABAP 7.40+新语法中的VALUE和REDUCE操作符,如何高效处理复杂内表统计任务。通过实战案例展示,这些新语法能将代码量减少70%以上,显著提升开发效率和可读性,特别适合大数据量下的销售数据分析、分组统计等场景。 阅读详情

相关推荐

保姆级教程:手把手教你从ENSEMBL官网下载GRCh38/GRCh37的GTF注释文件(附网址规律总结)

本文提供从ENSEMBL官网下载GRCh38/GRCh37的GTF注释文件的详细教程,包括路径解析、版本号确定和下载命令示例。特别解析了GRCh37的特殊存储结构,并分享批量下载、文件校验等高级技巧,帮助生物信息学研究者高效获取基因组注释文件。

weixin_29289821的博客 209

[LeetCode]Word Break,解题报告

目录 目录 前言 题目 DFS 动态规划 前言 过完年回来状态一直就是懒懒散散的,之前想写的年终总结也一直再拖沓。所以,从今天起找回之前的状态,每日的工作计划没完成就不能休息。今天的任务中有两道LeetCode题目,其中一道动态规划的题目稍微难一点,这里记录一下解题过程。 题目 Given a string s and a dictionary of words di

小一的专栏 3582

别再乱改基础库了!手把手教你搞定uni-app小程序上传图片报错(chooseAndUploadFile权限篇)

本文详细解析了uni-app开发微信小程序时遇到的`chooseAndUploadFile`权限报错问题,并提供了四步解决方案。从隐私协议与API权限的关联入手,指导开发者正确配置后台声明、双重提交策略和版本管理,避免降级基础库等常见误区,确保小程序合规运行。

weixin_33709364的博客 284

leetcode经典题目之word-break

题目: 链接:https://www.nowcoder.com/questionTerminal/5f3b7bf611764c8ba7868f3ed40d6b2c?answerType=1&f=discussion 来源:牛客网 给定一个字符串s和一组单词dict,判断s是否可以用空格分割成一个单词序列,使得单词序列中所有的单词都是dict中的单词(序列可以包含一个或多个单词)。 例如: 给定s=“leetcode”; dict=["leet", "code"]. 返回true,因为"leetco

qq_41634872的博客 527

GooFishCrawlers实战:破解闲鱼虚拟列表与登录态爬取难题

本文详细介绍了GooFishCrawlers项目如何破解闲鱼爬虫三大核心难题:通过Cookie持久化实现自动化登录,模拟人工滚动策略完整抓取虚拟列表数据,以及配置浏览器选项进行基础反反爬伪装。项目实战演示了从环境搭建、登录态维持到数据解析落地的完整流程,为开发者提供了稳定抓取闲鱼商品数据的解决方案。

weixin_29091445的博客 636

通过python+openCV实现对图片中箭头方向的判断

摘要: 针对YOLOv8算法在箭头方向识别效果不佳的问题,提出了一种基于OpenCV的解决方案。该方法通过图像预处理、轮廓分析、几何特征提取(凸包、质心、外接矩形)和方向向量计算,准确判断箭头的上下左右方向。代码示例展示了从图像读取到方向识别的完整流程,并提供了优化建议(如调整二值化阈值、处理多轮廓)。该方案在Windows11+Python3.12+OpenCV环境下验证有效,适用于需要简单几何特征分析的箭头方向检测场景。

AnChenliang_1002的博客 708

LeetCode Word Break II

原题链接在这里:https://leetcode.com/problems/word-break-ii/ 本题是Word Break的进阶题,要返回所有wordbreak的结果。 本题参照了这篇帖子:http://www.cnblogs.com/springfor/p/3877056.html 首先更具isWordBreak判断是否能拆,若能拆看从头到尾走,找到了一个wordDict里包含的

Dylan_Java_NYC的博客 619

YOLOv11改进策略【模型轻量化】| 替换骨干网络为 2024轻量化网络MoblieNetV4:移动生态系统的通用模型

一、本文介绍 本文记录的是基于MobileNet V4的YOLOv11目标检测轻量化改进方法研究。MobileNet V4通过整合UIB、Mobile MQA以及优化的NAS策略,能够在在不降低性能指标的前提下,降低计算成本。本文配置了原论文中MNv4-Conv-S、MNv4-Conv-M、MNv4-Conv-L、MNv4-Hybrid-M和MNv4-Hybrid-L五种模型,以满足不同的需求。 模型 参数量 计算量 推理速度 YOLOv11m 20.0M 67.6GFLOPs 3.5ms

Limiiiing的博客 4476

Word Break II -- LeetCode

Word Break II -- LeetCode

qq_44939824的博客 111

【Windows Server 2019】存储服务器的配置和管理——iSCSI的安装和配置(下)

博文介绍了如何使用Windows 10 和 ESXi 验证iSCSI服务器部署成功以及如何管理iSCSI服务 。

VMware, Windows, Linux, Cisco, Container, Kubernetes, Kali 8008

ParaView :在paraview创建渲染视图

ParaView :在paraview创建渲染视图

希望我的博客,能帮上你解决学习中工作中所遇到的问题 1350

[Leetcode]Word Break & Word Break II

Word Break is a problem that gives you a string and a dict of word, you have to indicate that if the string

Toni Xun的博客 760

am62x A53与R5F通信

AM62x是Ti的一款异构多核芯片。remoteproc驱动,硬编码(写死)了R5F/M4F固件的名字,这样驱动启动时,就会找硬件编码的固件,下面是其对应关系。值得注意的是,linux内核中的M4F remoteproc驱动启动M4F,而linux内核中的R5F remoreteproc驱动不启动R5F,相反,它attach到已经在boot阶段启动的R5F固件上。在/lib/firmware的真实固件名:,而我们生成的固件名如下所示,没有直接对应的,经确认,.out文件就是我们需要的,重命名后即可。

鹰之翔 1367

leetcode word-break-ii(Java)

leetcode题目 word-break-ii 题目描述 * Given a string s and a dictionary of words dict, * add spaces in s to construct a sentence where each word is a valid dictionary word. * Return all such possible ...

聚沙成塔 454

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

[亚麻高频题] Leetcode 140. Word Break II

题目描述&链接 Leetcode 140:是Leetcode 139. Word Break进阶版,LC139求得是可行性,这道题求得是所有方案。 相似题目 Leetcode 139:Word Break Leetcode 472: Concatenated Words 题目思路 1.DFS搜索 既然要求所有方案就避免不了全局遍历,我们可以依照与LC139类似思路求解,从头遍历来寻找匹配的单词,然后从匹配单词后面的index继续搜索,或者跳过这次匹配就这向下找更长的匹配单词,直...

ok1382038的博客 489

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

[leetcode]140. Word Break II

链接:https://leetcode.com/problems/word-break-ii/description/ Given anon-emptystringsand a dictionarywordDictcontaining a list ofnon-emptywords, add spaces insto construct a sentence wh...

xiaocong1990的博客 458

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

LeetCode 140. Word Break II Solution 题解

Leetcode 140. Word Break II @(LeetCode)[LeetCode | Algorithm | DP | Backtracking | DFS] Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add spaces in s ...

TooSchoolForCool 1224

140. Word Break II(js)

140.Word Break II Given anon-emptystringsand a dictionarywordDictcontaining a list ofnon-emptywords, add spaces insto construct a sentence where each word is a valid dictionary word.Retur...

weixin_30865427的博客 122

leetcode python word-break-ii

第一篇博客word-break-ii题意思路暴力dfs python实现dp+dfs python实现 word-break-ii Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word. R...

clearsunshinel的博客 246

Word Break II - LeetCode

Word Break II - LeetCode 题目: Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add spaces in s to construct a sentence where each word is a valid dictionary w...

SGAFPZ的博客 572

算法 之 word break II

写这篇博客的原因是,对于要输出打印所有结果的题目: 比如:微软面试100题中的第4题, 在二元树中找出和为某一值的所有路径(树) 题目:输入一个整数和一棵二元树。 从树的根结点开始往下访问一直到叶结点所经过的所有结点形成一条路径。 打印出和与输入整数相等的所有路径。 例如 输入整数 22 和如下二元树 10 / \ 5 12 / \ 4 7 则打印出两条路径:10, 1

绝知此事要躬行 2600

Leetcode 140 Word Break II(DFS+dp)

题目连接:Leetcode 140 Word Break II解题思路:递归遍历,每次枚举一个词,并且在剩下的位置可被切割的情况递归,作为剪枝。class Solution { public: bool dfs(int d, string s, string t, vector&lt;string&gt;&amp; wordDict, vector&lt;string&gt;&amp; an...

不慌不忙、不急不躁 505

Leetcode 140. Word Break II (python+cpp)

Leetcode 140. Word Break II题目解法1:动态规划+暴力递归解法2:recursion+memorization总结 题目 解法1:动态规划+暴力递归 首先用word break I的方法来判断是否能够拆分 能拆分之后,暴力recursion解出所有答案 class Solution: def wordBreak(self, s: str, wordDict: List[str]) -> List[str]: def check(s):

qq_37821701的博客 725

【Leetcode】Word Break II

题目链接:https://leetcode.com/problems/word-break-ii/ Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word.Return all such po

业精于勤,荒于嬉 1066
上一篇: leetcode题目:Linked List Cycle和Linked List Cycle II
下一篇: leetcode题目:Copy List with Random Pointer
晓风ABC
博客等级 码龄15年 1粉丝 18原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值