当前位置:网站首页>leetcode:139. 单词拆分【dfs + memory】
leetcode:139. 单词拆分【dfs + memory】
2022-06-29 15:37:00 【白速龙王的回眸】

分析
一个个选,如果是前缀就可以选
如果在里面就返回True
记忆化参数,某个s
结果:能否由这个dict组成,返回bool
先特判in
然后一个个dict里面的word看,如果是前缀
就dfs去掉前缀的,如果为真,就返回True
如果全部都不返回True,返回False
这个就是把能找的都找了,实在返回不了True也就没有办法了哦
ac code
class Solution:
def wordBreak(self, s: str, wordDict: List[str]) -> bool:
@cache
def dfs(s):
if s in wordDict:
return True
for word in wordDict:
if s.startswith(word):
if dfs(s[len(word):]):
return True
return False
return dfs(s)
总结
dfs记忆化搜索优雅如斯
边栏推荐
- Several imaging modes of polarimetric SAR
- 12.udp protocol -bite
- 11. application layer data transmission format / port number -bite
- 13.TCP-bite
- C learning 2: heap and stack
- Volcano engine was selected into the first "panorama of edge computing industry" in China
- Imgutil image processing tool class, text extraction, image watermarking
- 微信公告号自动回复使用图灵机器人实现智能回复
- Neural network for remote sensing image processing
- Huawei cloud AOM version 2.0 release
猜你喜欢

企业转型升级之道:数字化转型,思想先行

架构实战营模块五作业

CVPR 2022 | greatly reduce the manual annotation required for zero sample learning. Mapuosuo and Beiyou proposed category semantic embedding rich in visual information

Autodesk Revit 2023软件安装包下载及安装教程

火山引擎入选国内首个《边缘计算产业全景图》

golang操作etcd

It is expected to significantly improve the computational performance of integrated photonic circuits. The Tsinghua team proposed a diffraction pattern neural network framework

postgresql源码学习(24)—— 事务日志⑤-日志写入WAL Buffer

从第三次技术革命看企业应用三大开发趋势

C # learning 1: value type and reference type
随机推荐
A. Beat The Odds
微信公共号开发,发送消息回复文本
Numpy 的研究仿制 1
About sql+nosql: newsql database
Neural network for remote sensing image processing
【crossbeam系列】5 crossbeam-util和crossbeam-queue:一些实用的小东西
"Game engine shallow in shallow out" 98 Substancepainer plug-in development
Why MySQL chooses b+ tree to store indexes
C. Most Similar Words
微博评论高可用高性能计算架构
华为云AOM 2.0版本发布
. Net program configuration file operation (INI, CFG, config)
golang gopsutil库的使用:进程和系统资源监控(CPU 内存 磁盘等)
从第三次技术革命看企业应用三大开发趋势
Applet judges that the data is not empty
postgresql源码学习(24)—— 事务日志⑤-日志写入WAL Buffer
Pre war minesweeping: five measures for vulnerability management
Summary of recent work
12.UDP协议-bite
#夏日挑战赛# HarmonyOS - 方舟开发框架ArkUI 流光按钮效果