当前位置:网站首页>139. word splitting
139. word splitting
2022-06-23 15:47:00 【Zzu dish】
To give you one character string s And a String list wordDict As a dictionary . Please judge whether you can use the words in the dictionary to splice s .
Be careful : It is not required to use all the words in the dictionary , And the words in the dictionary can be reused .
Example 1:
Input : s = “leetcode”, wordDict = [“leet”, “code”]
Output : true
explain : return true because “leetcode” Can be “leet” and “code” Stitching into .
Example 2:
Input : s = “applepenapple”, wordDict = [“apple”, “pen”]
Output : true
explain : return true because “applepenapple” Can be “apple” “pen” “apple” Stitching into .
Be careful , You can reuse the words in the dictionary .
Example 3:
Input : s = “catsandog”, wordDict = [“cats”, “dog”, “sand”, “and”, “cat”]
Output : false
reflection
Definition dp The meaning of array and its subscript
dp[i]: Express wordDict front 1-i Can a letter be made up of words
- dp[i] =1 , On behalf of 1-i Letters can be made up of words
- dp[i] = 0, conversely
initialization dp Array
establish dp[ s.length + 1] Array
Make dp[ 0 ] =1;
here dp[0] Yes, it plays an auxiliary role , The main purpose is to make dp[i-word.lenth] , Its i =word.length when ,dp = 1;
State transition equation
Here if dp[i-word.length] = 1, And the words and word identical dp[i]=1
class Solution {
public boolean wordBreak(String s, List<String> wordDict) {
// step 1 establish dp Array
int[] dp=new int[s.length()+1];
// step 2 initialization dp Array
dp[0] =1;
// step 3 perfect dp Array
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;
}
}
边栏推荐
- golang 重要知识:定时器 timer
- 【Pyside2】 pyside2的窗口在maya置顶(笔记)
- How to open a stock account? Is online account opening safe?
- 为什么高通滤波器也能变成微分器?
- JS create an array (literal)
- Detailed steps for MySQL dual master configuration
- Horizon development board commissioning
- JS垃圾回收
- Memory Consistency and Cache Coherence —— 内存一致性
- Explain in detail the principle and implementation of redis distributed lock
猜你喜欢

Six programming insights in these five years!

js中的push函数介绍

Moher College - manual SQL injection vulnerability test (MySQL database)

MySQL series: overview of the overall architecture

This year's cultural entertainers have turned their sidelines into their main business

golang 重要知识:定时器 timer

Convert JSON file of labelme to coco dataset format

JS garbage collection

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

Important knowledge of golang: sync Once explanation
随机推荐
Three simple tips for accelerating yarn install
Volatile~ variables are not visible under multithreading
[普通物理] 半波损失 等厚与等倾干涉
php 二维数组插入
Matlab| sparse auxiliary signal denoising and pattern recognition in time series data
股票开账户如何优惠开户?在线开户安全么?
32. compose beautiful touch animation
[MAE]Masked Autoencoders掩膜自编码器
js的slice()和splice()
labelme的JSON文件转成COCO数据集格式
任何代码未动的情况下第二天项目访问速度明显下降,案例分析
[opencv450] salt and pepper noise demo
List query sorting parameter processing
JS的unshift()和shift()
Idea view View the class file idea Class folder
139. 单词拆分
2022年个人理财利率是多少?个人如何选择理财产品?
Sectigo(Comodo)证书的由来
mysql主从只同步部分库或表的思路与方法
freemark 使用ftl文件 生成word