当前位置:网站首页>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的文章中~
边栏推荐
- 美团二面:Redis与MySQL双写一致性如何保证?
- 动态上传jar包热部署
- 矩阵数学原理
- 中日颜色风格
- [Happy Qixi Festival] How does Nacos realize the service registration function?
- Mathematical Principles of Matrix
- 大师教你3D实时角色制作流程,游戏建模流程分享
- 招标公告 | 海纳百创公众号运维项目
- Senior game modelers tell newbies, what are the necessary software for game scene modelers?
- Getting started with 3D modeling for games, what modeling software can I choose?
猜你喜欢

美团二面:Redis与MySQL双写一致性如何保证?

956. 最高的广告牌

【手撕AHB-APB Bridge】~ AMBA总线 之 AHB

从单体架构迁移到 CQRS 后,我觉得 DDD 并不可怕

Develop a SpaceX website based on the Appian low-code platform

三大技巧让你成功入门3D建模,零基础小白必看

2022 Niu Ke Summer Multi-School Training Camp 5 (BCDFGHK)

Essential knowledge for entry-level 3D game modelers

堪称奔驰“理财产品”,空间媲美宝马X5,采用了非常运动的外观
![[Happy Qixi Festival] How does Nacos realize the service registration function?](/img/df/5793145da45bc80d227b0babfac914.png)
[Happy Qixi Festival] How does Nacos realize the service registration function?
随机推荐
中日颜色风格
mysql基础
怎么将自己新文章自动推送给自己的粉丝(巨简单,学不会来打我)
KT6368A Bluetooth certification problem_FCC and BQB_CE_KC certification or other instructions
[Happy Qixi Festival] How does Nacos realize the service registration function?
Day118.尚医通:订单列表、详情、支付
MySQL基础篇【子查询】
VMware NSX 4.0 -- 网络安全虚拟化平台
关于使用read table 语句
uniapp横向选项卡(水平滚动导航栏)效果demo(整理)
头脑风暴:完全背包
【LeetCode】图解 904. 水果成篮
加解密在线工具和进制转化在线工具
Couple Holding Hands [Greedy & Abstract]
Literature reading ten - Detect Rumors on Twitter by Promoting Information Campaigns with Generative Adversarial Learn
@Import注解的作用以及如何使用
【无标题】线程三连鞭之“线程池”
Xiaohei's leetcode journey: 95. Longest substring with at least K repeating characters
DNS常见资源记录类型详解
MySQL的安装与卸载