DP,将链表wordDict改成哈希表
Set<String> hashtable = new HashSet(wordDict);
class Solution {
public boolean wordBreak(String s, List<String> wordDict) {
int len = s.length();
boolean[] dp = new boolean[len+1];
Set<String> hashtable = new HashSet(wordDict);
dp[0] = true;
for(int i = 1;i <= len;i++){
for(int j = 0;j < i;j++){
//substring(start,end)
if(dp[j] && hashtable.contains(s.substring(j,i))){
dp[i] = true;
break;
}
}
}
return dp[len];
}
}
1589

被折叠的 条评论
为什么被折叠?



