当前位置:网站首页>leetcode经典例题——单词拆分
leetcode经典例题——单词拆分
2022-08-04 23:50:00 【你食不食油饼】
题目描述:
1、动态规划
思路:这道题是要我们单词拆分来,能用字符串列表这个数组组成,我们就可以用到动态规划:
初始化 dp=[false,……,false],长度为 n+1。n 为字符串长度。dp[i] 表示 s 的前 i 位是否可以用 wordDict 中的单词表示。初始化 dp[0]=True,空字符可以被表示。
当我们遍历s单词字符串时,如果dp[i] =false,则退出循环,证明我们无法拼接到这个位置;
如果dp[i] = true,我们遍历字符串列表,依次匹配,看是否能匹配至s单词字符串,匹配成功,则设置该位置dp[word.length+1] = true;
看看代码:
public boolean wordBreak(String s, List<String> wordDict) {
int len = s.length();
boolean[] dp = new boolean[len+1];
dp[0] = true;
for(int i = 0;i<len;i++){
if(!dp[i]) continue;
for(String word : wordDict){
while ( s.startsWith(word,i)){
dp[word.length()+i] = true;
break;
}
}
}
return dp[len];
}
时间复杂度:O(m*n),m为单词s的长度,n为字典wordDict的数量
空间复杂度:O(n)
2、回溯算法
思路:这道题既然是依次拼接,也就是一道排列组合题,我们也可以用回溯算法来解决
回溯算法就也是利用遍历wordDict,判断s.startsWith(word,i)为true时进行递归,
如果该次递归成功,我们返回结果true;
如果该次递归不成功,我们回溯,仍旧遍历wordDict进行判断是否下一次递归,直到wordDict遍历完成,仍没结果才返回false;
进入代码:
public boolean wordBreak(String s, List<String> wordDict) {
return backTrack(0,s,wordDict);
}
public boolean backTrack(int len,String s,List<String> wordDict){
if (len == s.length()) return true;
for (String word : wordDict) {
if (s.startsWith(word,len)){
if (backTrack(len+word.length(),s,wordDict))
return true;
}
}
return false;
}
注:该回溯算法有一个弊端,在leetcode运行时,由于它设置一个单词s为aaaaaaaaaaaaaa……wordDict["a","aa","aaa",……],我们用回溯算法时,由于会优先进行第一个"a"的匹配,所以可能出现超时情况!
持续更新关于leetcode的文章中~
边栏推荐
- Flutter启动流程(Skia引擎)介绍与使用
- How to automatically push my new articles to my fans (very simple, can't learn to hit me)
- Chinese and Japanese color style
- Statistical words (DAY 101) Huazhong University of Science and Technology postgraduate examination questions
- 【CVA估值训练营】财务建模指南——第一讲
- uniapp 分享功能-分享给朋友群聊朋友圈效果(整理)
- Literature reading ten - Detect Rumors on Twitter by Promoting Information Campaigns with Generative Adversarial Learn
- 【数据挖掘概论】数据挖掘的简单描述
- 【无标题】线程三连鞭之“线程池”
- LeetCode Hot 100
猜你喜欢
什么是次世代建模(附学习资料)
话题 | 雾计算和边缘计算有什么区别?
年薪50W+的测试工程师都在用这个:Jmeter 脚本开发之——扩展函数
NebulaGraph v3.2.0 Release Note,对查询最短路径的性能等多处优化
【LeetCode】滑动窗口题解汇总
Statistical words (DAY 101) Huazhong University of Science and Technology postgraduate examination questions
MySQL基础篇【子查询】
【LeetCode】双指针题解汇总
KT6368A Bluetooth certification problem_FCC and BQB_CE_KC certification or other instructions
App测试和Web测试的区别
随机推荐
【数据挖掘概论】数据挖掘的简单描述
jenkins发送邮件系统配置
KT6368A蓝牙的认证问题_FCC和BQB_CE_KC认证或者其它说明
线程三连鞭之“线程的状态”
再肝3天,整理了90个 NumPy 例子,不能不收藏!
After another 3 days, I have sorted out 90 NumPy examples, and I can't help but bookmark it!
ClickHouse 二级索引
Handwritten Distributed Configuration Center (1)
NebulaGraph v3.2.0 Release Note,对查询最短路径的性能等多处优化
How to automatically push my new articles to my fans (very simple, can't learn to hit me)
Pytorch分布式训练/多卡/多GPU训练DDP的torch.distributed.launch和torchrun
[Happy Qixi Festival] How does Nacos realize the service registration function?
LeetCode Hot 100
npm基本操作及命令详解
Implementation principle of golang coroutine
【云原生--Kubernetes】Pod控制器
~ hand AHB - APB Bridge 】 【 AMBA AHB bus
Statistical words (DAY 101) Huazhong University of Science and Technology postgraduate examination questions
没有这些「伪需求」,产品经理的 KPI 怎么完成?
怎么将自己新文章自动推送给自己的粉丝(巨简单,学不会来打我)