Skip to content

refactor #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 25, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions C++/chapSearching.tex
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ \subsubsection{代码}
class Solution {
public:
bool searchMatrix(const vector<vector<int>>& matrix, int target) {
if (matrix.empty()) return false;
const size_t m = matrix.size();
const size_t n = matrix.front().size();

Expand Down
25 changes: 10 additions & 15 deletions C++/chapStackAndQueue.tex
Original file line number Diff line number Diff line change
Expand Up @@ -104,27 +104,22 @@ \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;
vector<int> f(s.size(), 0);
int ret = 0;
int* d = new int[s.size()];
d[s.size() - 1] = 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 + f[i + 1] + 1;
// case: "((...))"
if (s[i] == '(' && match < s.size() && s[match] == ')') {
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]);
}
return ret;
}
Expand Down