1. 程式人生 > >動態規劃之139 Word Break

動態規劃之139 Word Break

題目連結:https://leetcode-cn.com/problems/word-break/

參考連結:https://blog.csdn.net/c_flybird/article/details/80703494

     http://www.cnblogs.com/springfor/p/3874731.html

這種題目一般出現字串、子陣列都需要使用動態規劃

dp[i],前i個字串是否在字典中。

dp[0]=true;空字元肯定是在字典中。

public boolean wordBreak(String s, List<String> wordDict) {
        boolean
dp[]=new boolean[s.length()+1]; Arrays.fill(dp, false); dp[0]=true; for (int i = 1; i < dp.length; i++) { for (int j = i - 1; j >= 0; j--) { if (dp[j] && wordDict.contains(s.substring(j, i))) { dp[i]
= true; break; } } } return dp[s.length()]; }