Skip to content

Instantly share code, notes, and snippets.

@kanrourou
Created October 18, 2017 05:55
class Solution {
public:
int longestValidParentheses(string s) {
int maxLen = 0, left = 0;
stack<int> stack;
for(int i = 0; i < s.size(); ++i)
{
if(stack.empty() && s[i] == ')')
{
left = i + 1;
continue;
}
if(s[i] == '(')
stack.push(i);
else
{
stack.pop();
int len = stack.empty()? i - left + 1: i - stack.top();
maxLen = max(maxLen, len);
}
}
return maxLen;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment