当前位置:网站首页>139. 單詞拆分
139. 單詞拆分
2022-06-23 15:47:00 【zzu菜】
給你一個字符串 s 和一個字符串列錶 wordDict 作為字典。請你判斷是否可以利用字典中出現的單詞拼接出 s 。
注意:不要求字典中出現的單詞全部都使用,並且字典中的單詞可以重複使用。
示例 1:
輸入: s = “leetcode”, wordDict = [“leet”, “code”]
輸出: true
解釋: 返回 true 因為 “leetcode” 可以由 “leet” 和 “code” 拼接成。
示例 2:
輸入: s = “applepenapple”, wordDict = [“apple”, “pen”]
輸出: true
解釋: 返回 true 因為 “applepenapple” 可以由 “apple” “pen” “apple” 拼接成。
注意,你可以重複使用字典中的單詞。
示例 3:
輸入: s = “catsandog”, wordDict = [“cats”, “dog”, “sand”, “and”, “cat”]
輸出: false
思考
定義dp數組及其下標的含義
dp[i]:錶示wordDict前1-i個字母能否由單詞組成
- dp[i] =1 ,代錶前1-i個字母可以由單詞組成
- dp[i] = 0,反之
初始化dp數組
創建dp[ s.length + 1]數組
令dp[ 0 ] =1;
這裏dp[0]是起輔助作用,主要目的為的是讓 dp[i-word.lenth] ,其i =word.length時,dp = 1;
狀態轉移方程
這裏如果dp[i-word.length] = 1,並且截下來的詞和word相同dp[i]=1
class Solution {
public boolean wordBreak(String s, List<String> wordDict) {
// step 1 創建dp數組
int[] dp=new int[s.length()+1];
// step 2 初始化dp數組
dp[0] =1;
// step 3 完善dp數組
for (int i=1;i<dp.length;i++){
for (String word : wordDict) {
if(i>=word.length()){
if (dp[i-word.length()]==0) continue;
String sub=s.substring(i-word.length(),i);
if(sub.equals(word)) {
dp[i]=1;
break;
}
}
}
}
// step 4 printDp
for (int i = 0; i < dp.length; i++) {
System.out.println("dp "+i+","+dp[i]);
}
if (dp[dp.length-1]==1) return true;
else return false;
}
}
边栏推荐
- Top 10 purchase, sales and inventory software rankings!
- golang 重要知识:定时器 timer
- Force deduction solution summary 513- find the value of the lower left corner of the tree
- 32. Compose 优美的触摸动画
- Android kotlin collaboration Async
- Big factory Architect: how to draw a grand business map?
- Diffraction of light
- [opencv450] salt and pepper noise demo
- 详解Redis分布式锁的原理与实现
- Matlab| sparse auxiliary signal denoising and pattern recognition in time series data
猜你喜欢

Pop() element in JS
Redis缓存三大异常的处理方案梳理总结

Raspberry PI installing the wiring pi

MySQL series: overview of the overall architecture

Convert JSON file of labelme to coco dataset format

A transformer can only convert alternating current. How can I convert direct current?
Sorting out and summarizing the handling schemes for the three major exceptions of redis cache

自监督学习(SSL)Self-Supervised Learning

JSON——学习笔记(消息转换器等)

How strong is Jingdong's takeout after entering meituan and starving the hinterland?
随机推荐
MySQL series: overview of the overall architecture
Usestate vs useref and usereducer: similarities, differences and use cases
MySQL create and manage tables
2022年个人理财利率是多少?个人如何选择理财产品?
[opencv450] salt and pepper noise demo
Wechat applet guides users to add applet animation page
labelme的JSON文件转成COCO数据集格式
【opencv450】椒盐噪声demo
Important knowledge of golang: sync Cond mechanism
他山之石 | 微信搜一搜中的智能问答技术
JS创建一个数组(字面量)
打印内存站信息
Gartner's latest report: development of low code application development platform in China
Moher College - manual SQL injection vulnerability test (MySQL database)
ABP框架之——数据访问基础架构(下)
Important knowledge of golang: waitgroup parsing
Embedded software architecture design - program layering
【Pyside2】 pyside2的窗口在maya置顶(笔记)
Detailed steps for MySQL dual master configuration
【无标题】激光焊接在医疗中的应用