diff --git a/problems/0139.单词拆分.md b/problems/0139.单词拆分.md index 1653a81a..e04cb173 100644 --- a/problems/0139.单词拆分.md +++ b/problems/0139.单词拆分.md @@ -89,27 +89,26 @@ class Solution { private: bool backtracking (const string& s, const unordered_set& wordSet, - vector& memory, + vector& memory, int startIndex) { if (startIndex >= s.size()) { return true; } // 如果memory[startIndex]不是初始值了,直接使用memory[startIndex]的结果 - if (memory[startIndex] != -1) return memory[startIndex]; + if (!memory[startIndex]) return memory[startIndex]; for (int i = startIndex; i < s.size(); i++) { string word = s.substr(startIndex, i - startIndex + 1); if (wordSet.find(word) != wordSet.end() && backtracking(s, wordSet, memory, i + 1)) { - memory[startIndex] = 1; // 记录以startIndex开始的子串是可以被拆分的 return true; } } - memory[startIndex] = 0; // 记录以startIndex开始的子串是不可以被拆分的 + memory[startIndex] = false; // 记录以startIndex开始的子串是不可以被拆分的 return false; } public: bool wordBreak(string s, vector& wordDict) { unordered_set wordSet(wordDict.begin(), wordDict.end()); - vector memory(s.size(), -1); // -1 表示初始化状态 + vector memory(s.size(), 1); // -1 表示初始化状态 return backtracking(s, wordSet, memory, 0); } };