139. Word Break (DP)
打印字符串的一个字符用str.charAt()
substring用str.subString()
要用这个字母之前几个能否组成word这样来考虑
https://leetcode.com/problems/word-break/discuss/43790/Java-implementation-using-DP-in-two-ways
1 class Solution { 2 public boolean wordBreak(String s, List<String> wordDict) { 3 int n = s.length(); 4 boolean[] res = new boolean[n + 1]; 5 res[0] = true; 6 for(int i = 1; i < n + 1; i++) { 7 for(int j = i - 1; j >= 0; j--) { 8 if(res[j] && wordDict.contains(s.substring(j, i))) { 9 res[i] = true; 10 break; 11 } 12 res[i] = false; //boolean数组默认false 13 } 14 } 15 return res[n]; 16 } 17 }