class Solution {
public:
int longestValidParentheses(string s) {
int n = s.length();
vector<int> stack;
int maxLength = 0;
int last = -1;
for(int i = 0; i < n; ++i) {
if(s[i] == '(') {
stack.emplace_back(i);
} else {
if(stack.empty()) {
last = i;
} else {
stack.pop_back();
if(stack.empty()) {
maxLength = max(maxLength, i - last);
} else {
maxLength = max(maxLength, i - stack[stack.size() - 1]);
}
}
}
}
return maxLength;
}
};
4/05/2014
Leetcode -- Longest Valid Parentheses
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment