【LeetCode】Word Break 解题报告

【项目实战】AES实施应符合NIST密码算法验证程序 AES由美国国家标准与技术研究院(NIST)制定。NIST是负责开发和维护美国政府使用的技术标准的机构,包括加密标准。 阅读详情

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".

==================== 暴力法、穷尽法 ====================

【思路】

这道题其实是水过的。

我直接的想法是,从s的第一个字母开始,逐个增加字母去dict里找,如果找到,就继续从下一个位置开始,逐个增加字母去dict里找,如果没找到,就返回false。但是这样算法不争取,比如 s="aaaaaaa",dict=["aaa", "aaaa"],按上面算法返回false,但实际上是可以分割成两个单词的。

错误的原因很简单,有漏掉情况。于是我想到采用暴力的方法, 把所有可能的组合都找出来,只要有组合成功的就返回true。代码如下:

class Solution {
    boolean ret;
    
    public boolean wordBreak(String s, Set<String> dict) {
        String[] all = dict.toArray(new String[0]);
        ret = false;
        nextWord(0, s, all);
        return ret;
    }
    
    void nextWord(int pos, String s, String[] all) {
    	if (ret) return;
        if (pos == s.length()) ret = true;
        
        for (int i = 0; i < all.length; i++) {
            if (s.indexOf(all[i], pos) == pos) {
                nextWord(pos + all[i].length(), s, all);
            }
        }
    }
}

但是,这样会超时。LeetCode给出了超时的样例:

s=“aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab”

dict=["a", "aa", "aaa", "aaaa", "aaaaa", "aaaaaa", "aaaaaaa"]

我一看,重点是最后哪一个b,于是我想到了,遍历s中的所有字符,看看有没有在dict中没有出现的,如果dict中所有单词都不含这个字符,那么s肯定是不可分解的。

基于此我增加了部分代码:

【Java代码】

class Solution {
    boolean ret;
    
    public boolean wordBreak(String s, Set<String> dict) {
        String[] all = dict.toArray(new String[0]);
        
        //////////////////////////////////////////////////
        //如果s中有字母没在dict出现过,那么结果肯定为false
        for (int i = 0; i < s.length(); i++) {
            boolean flag = false;
            for (int j = 0; j < all.length; j++) {
                if (all[j].indexOf(s.charAt(i)) > -1) {
                    flag = true;
                    break;
                }
            }
            if (!flag) {
                return false;
            } 
        }
        //////////////////////////////////////////////////
        
        ret = false;
        nextWord(0, s, all);
        return ret;
    }
    
    void nextWord(int pos, String s, String[] all) {
    	if (ret) return;
        if (pos == s.length()) ret = true;
        
        for (int i = 0; i < all.length; i++) {
            if (s.indexOf(all[i], pos) == pos) {
                nextWord(pos + all[i].length(), s, all);
            }
        }
    }
}

说这道题是水过的就是因为LeetCode有提示不通过样例,这样我就能根据样例输入输出来修改代码。


【华为OD机试真题】459、小扇和小船的数字游戏、数字游戏 | 机试真题+思路参考+代码解析(CD卷)(C++、Java、Py) ### 🎃题目描述 >小扇和小船今天又玩起来了数字游戏,小船给小扇一个正整数 n(1  现在求 m 的最小值 阅读详情

相关推荐

华为昇腾Atlas 200I DK A2实战:从开箱到运行首个AI应用

本文详细介绍了华为昇腾Atlas 200I DK A2开发板从开箱到运行首个AI应用的全过程。包括硬件准备、系统烧录、开发板连接方案、AI应用运行及性能优化技巧,帮助开发者快速上手部署AI项目,提升开发效率。

weixin_27038245的博客 603

leetcode题解:第139题Word Break

https://leetcode-cn.com/problems/word-break/ 文章目录解法一、回溯代码解法二、动态规划代码复杂度分析 解法一、回溯 依照惯例,没有好的思路的时候,就先想想暴力解法怎么做。暴力解法无非是逐个单词地拆分字符串,每次选的单词可以重复。要实现暴力解法的话,肯定就是用回溯,因为回溯算法“撤销选择”的特点,保证了每次都能考虑到所有的单词。 简而言之,对于字符串s和单词word,word长度为n,当word与s[:n]相同时,就可以把word选来进行一次拆分,紧接着继续判断s[

chenf1999的博客 542

BIOS/UEFI完全指南:从开机自检到超频魔改的底层硬件控制

计算机启动过程始于固件层,其核心是负责硬件初始化与引导的系统软件。传统BIOS基于16位实模式,寻址能力有限,而现代UEFI采用保护模式,支持图形界面与安全启动,实现了从概念到架构的革新。这一演进带来了显著的技术价值:更快的启动速度、更强的安全性以及对大容量存储设备的原生支持。在工程实践中,无论是解决“虚拟化技术禁用”问题、调整内存XMP配置以提升性能,还是为老主板“魔改”添加NVMe支持,都需要深入理解并操作BIOS/UEFI设置。掌握这些底层交互能力,是进行硬件调试、系统优化与故障排查的关键,也是连接抽

weixin_34236869的博客 459

LeetCode——word-break(单词切分)

知识点:动态规划 本文首发于github 题目描述 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 =“leetc...

ai-exception的博客 614

【yolov8】与yolov5的区别及改进详解

yolov8-----有点厉害的目标检测框架.

qq_44878985的博客 3万+

AntV G6边动画实战:手把手教你实现数据流向箭头与虚线流动效果

本文详细解析了如何使用AntV G6实现数据流向箭头与虚线流动效果,通过自定义边动画提升数据可视化的动态表现力。从基础环境搭建到核心概念讲解,再到虚线流动动画和箭头路径动画的具体实现与参数调控,帮助开发者掌握G6边动画的高级技巧,适用于网络流量监控、数据传递等场景。

weixin_28487433的博客 271

[亚麻高频题] Leetcode 139. Word Break

题目描述&链接 Leetcode 139:判断字典中的字符能否合并成目标字符串 -这道题只用判断可行性。进阶版Word Break II是需要返回所有可能情况。 Note:这题确实出的好,让你同时需要考虑算法和优化处理,这道题AC之路着实坎坷,屡屡尝试都TLE了,最后学会了优化解结果去Lintcode上写又TLE,最后终于弄明白最优解。。。 相似题目 Leetcode 140:Word Break II Leetcode 472: Concatenated Words 题目...

ok1382038的博客 542

COCO2017数据集类别分布深度解析

本文深入解析了COCO2017数据集的类别分布情况,包括80个常见物体的分类统计和样本数量分析。通过对比训练集与验证集的分布一致性,探讨了数据均衡性问题,并针对高频和低频类别提出了实用的数据预处理和模型训练建议,帮助优化计算机视觉任务的性能。

weixin_42533751的博客 375

leetCode !! word break

方法一:字典树+动态规划首先,创建node类,每个对象应该包含:一个node array nexts(如果有通往’a’的路,那么对应的nexts[0]就不该为null);一个boolean 变量(如果到达的这个字母恰好是字典中某个候选串的结尾,那么 标记end=true);然后,根据 字典,建立字典树;接着,使用动态规划的方式,检查目标字符串str是否可以用字典中的候选串拼出来。具体地,dp[i]表示 str[i…] 是否可以用字典中的候选串拼出来。

萝卜丝儿皮尔 695

win10安装openvpn客户端

进入C:\Program Files\TAP-Windows\bin目录,然后右键用管理员权限执行addtap.bat文件,此文件执行会创建一个网卡。链接:https://pan.baidu.com/s/1KAifTiP7qp1i5ZkfdnfwkA。4.2 进入安装目录的config下新建一个文件夹,将证书文件全部挪进去(不移动.ovpn文件)3.4 将拿到的ssl认证文件放入安装目录的config目录下。然后修改ovpn文件 “./”代表当前路径,第二个链接重复。3.1 一直下一步到这里打上勾。

qq_36200932的博客 3万+

MATLAB控制信号发生器(USB-GPIB-HS)GPIB仪器控制教程(含matlab代码)

自动化测试时需要自动产生信号,通过matlab控制信号发生器的频率与功率,以实现自动化信号生成输出

qq_43686057的博客 1617

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的博客 526

github创建账号第一步始终出错

github创建账号总结:第一次注册GitHub,填好用户名邮箱密码后进入下一步就卡在哪里,上面还提示Unable to verify your captcha response等,进入网址的解决办法以及下载其他的浏览器也无法解决,用手机浏览器抱着试一试的心态竟然成功了,分享给大家。手机下载谷歌浏览器,进入GitHub正常注册,能向下继续,邮箱验证完以后再用电脑登陆就ok啦

计算机毕业论文源码,学生个人网页制作html源码。贴近用户做网络推广和互联网优化。 4386

[leetcode] 139. Word Break 解题报告

题目链接:https://leetcode.com/problems/word-break/ 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.

小榕流光的专栏 1301

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

LeetCode Word Break解题报告

题目描述 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 =[...

Just The Way You Are 1543

vb 6.0并口开发操作实例

vb6并口开发,简体中文,试例内有inpout32.dll动态链接库

【LeetCode】140. Word Break II 解题报告(Python & C++)

作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录题目描述题目大意解题方法递归求解日期 题目地址:https://leetcode.com/problems/word-break-ii/ 题目描述 Given a non-empty string s and a dictionary wordDict containing a l...

负雪明烛 5825

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

[LeetCode]Word Break,解题报告

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

小一的专栏 3582

LeetCode Word Break 解题报告

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",

算法的天空 3886

[leetcode] 140. 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 al

小榕流光的专栏 1392

[LeetCode 解题报告]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.Return all such possible ...

CVer 204

LeetCode: Word Break II 解题报告

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 sentences. For ex...

weixin_33688840的博客 399

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 sentences. For example, given s = "cats

算法的天空 2366

【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 sentences. For example, given s = "

跳出温水的青蛙 3353

Word Break [Leetcode解题报告]

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”, “

点滴积累 336

LeetCode — Word Break 解题报告

题目解析: 判断给定的字符串能否用词典进行切分,切分的每一部分都是词典中的单词。这道题目最容易想到的策略就是O(n^2)的方法,两层for循环即可解决。外层循环i是单词的第一个位置到最后一个位置,内存循环j是从i-1到0,判断是否可以切分。

庖丁解牛 1649

LeetCode — Word Break II 解题报告

题目解析: 给定一个字符串以及一个词典,返回字符串合法的所有切分,所谓的合法就是单词都要在词典中出现。 这道题目很久之前是用递归进行解决的,一直是time exceed limited。最近又重新做了这道题目,感觉递归耗费的时间太多,于是就改成了循环的形式。以题目中的示例为准,建立如下二维数组:

庖丁解牛 1766
上一篇: 【LeetCode】LRU Cache 解题报告
下一篇: 【LeetCode】Word Break II 解题报告
ljiabin
博客等级 码龄17年 284粉丝 128原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值