From 9493f9437d0ff645282e1ae4ae449d83210207f3 Mon Sep 17 00:00:00 2001 From: JasonGitHub Date: Fri, 20 Dec 2013 21:47:22 +0800 Subject: [PATCH 1/3] refactor --- C++/chapStackAndQueue.tex | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/C++/chapStackAndQueue.tex b/C++/chapStackAndQueue.tex index bad09b28..abc1e93f 100644 --- a/C++/chapStackAndQueue.tex +++ b/C++/chapStackAndQueue.tex @@ -104,28 +104,26 @@ \subsubsection{使用栈} \subsubsection{Dynamic Programming, One Pass} \begin{Code} // LeetCode, Longest Valid Parenthese -// 时间复杂度O(n),空间复杂度O(1) -// @author 一只杰森(http://weibo.com/2197839961) +// 时间复杂度O(n),空间复杂度O(n) +// @author 一只杰森(http://weibo.com/wjson) class Solution { public: int longestValidParentheses(string s) { if (s.empty()) return 0; - int ret = 0; int* d = new int[s.size()]; d[s.size() - 1] = 0; + int ret = 0; for (int i = s.size() - 2; i >= 0; --i) { - if (s[i] == ')') d[i] = 0; - else { - int match = i + d[i + 1] + 1; // case: "((...))" - if (match < s.size() && s[match] == ')') { // found matching ')' - d[i] = match - i + 1; - if (match + 1 < s.size()) d[i] += d[match + 1]; // more valid sequence after j: "((...))(...)" - } else { - d[i] = 0; // no matching ')' - } + int match = i + d[i + 1] + 1; + if (s[i] == '(' && match < s.size() && s[match] == ')') { + d[i] = d[i + 1] + 2; + if (match + 1 < s.size()) d[i] += d[match + 1]; + } else { + d[i] = 0; } ret = max(ret, d[i]); } + delete[] d; return ret; } }; From 4e81197cc606cd88c2918a6079eb600cdaea3ff8 Mon Sep 17 00:00:00 2001 From: JasonGitHub Date: Sun, 22 Dec 2013 14:03:19 +0800 Subject: [PATCH 2/3] Refactored Longest Valid Parentheses --- C++/chapStackAndQueue.tex | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/C++/chapStackAndQueue.tex b/C++/chapStackAndQueue.tex index abc1e93f..2315b2ef 100644 --- a/C++/chapStackAndQueue.tex +++ b/C++/chapStackAndQueue.tex @@ -109,21 +109,18 @@ \subsubsection{Dynamic Programming, One Pass} class Solution { public: int longestValidParentheses(string s) { - if (s.empty()) return 0; - int* d = new int[s.size()]; - d[s.size() - 1] = 0; + vector f(s.size(), 0); int ret = 0; for (int i = s.size() - 2; i >= 0; --i) { - int match = i + d[i + 1] + 1; + int match = i + f[i + 1] + 1; + // case: "((...))" if (s[i] == '(' && match < s.size() && s[match] == ')') { - d[i] = d[i + 1] + 2; - if (match + 1 < s.size()) d[i] += d[match + 1]; - } else { - d[i] = 0; + f[i] = f[i + 1] + 2; + // if a valid sequence exist afterwards "((...))()" + if (match + 1 < s.size()) f[i] += f[match + 1]; } - ret = max(ret, d[i]); + ret = max(ret, f[i]); } - delete[] d; return ret; } }; From 3f189a32a74a2b29d1619fdafeebd7040edaf1eb Mon Sep 17 00:00:00 2001 From: JasonGitHub Date: Sun, 22 Dec 2013 22:53:02 +0800 Subject: [PATCH 3/3] Fixed a bug that might invoke undefined behavior Calling front() on an empty container would invoke undefined behavior --- C++/chapSearching.tex | 1 + 1 file changed, 1 insertion(+) diff --git a/C++/chapSearching.tex b/C++/chapSearching.tex index d668f0c1..e008d75b 100644 --- a/C++/chapSearching.tex +++ b/C++/chapSearching.tex @@ -179,6 +179,7 @@ \subsubsection{代码} class Solution { public: bool searchMatrix(const vector>& matrix, int target) { + if (matrix.empty()) return false; const size_t m = matrix.size(); const size_t n = matrix.front().size();