diff --git a/problems/0139.单词拆分.md b/problems/0139.单词拆分.md index 7ff13f72..5b38a7a5 100644 --- a/problems/0139.单词拆分.md +++ b/problems/0139.单词拆分.md @@ -234,11 +234,13 @@ Java: ```java class Solution { public boolean wordBreak(String s, List wordDict) { + HashSet set = new HashSet<>(wordDict); boolean[] valid = new boolean[s.length() + 1]; valid[0] = true; + for (int i = 1; i <= s.length(); i++) { - for (int j = 0; j < i; j++) { - if (wordDict.contains(s.substring(j,i)) && valid[j]) { + for (int j = 0; j < i && !valid[i]; j++) { + if (set.contains(s.substring(j, i)) && valid[j]) { valid[i] = true; } }