public boolean wordBreak(String s, List<String> wordDict) {
int i,j,k,l;
int n= s.length();
int maxn = 510;
int dp[] = new int[maxn];
for(i=0;i<n;i++){
for(j=0;j<wordDict.size();j++){
l = wordDict.get(j).length();
if(i-l+1<0){
continue;
}
if(i-l != -1 && dp[i-l]==0){
continue;
}
for(k=0;k<l;k++){
if(s.charAt(i-l+1+k)!=wordDict.get(j).charAt(k)){
break;
}
}
if(k==l){
dp[i]=1;
break;
}
}
}
if(dp[n-1] == 0){
return false;
}else{
return true;
}
}
LeetCode-139单词划分(动态规划)
最新推荐文章于 2024-09-08 19:37:30 发布