Create 32.cpp

This commit is contained in:
Kirigaya Kazuto 2018-06-19 14:02:33 +08:00 committed by GitHub
parent 843ac0e9bc
commit 498579d77d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

43
LeetCode-CN/32.cpp Normal file
View File

@ -0,0 +1,43 @@
#include <algorithm>
#include <stack>
class Solution {
public:
int longestValidParentheses(string s) {
int len = s.size();
int maxp = 0;
stack<int> stk;
for (int i = 0; i < len; i++)
{
if (s[i] == '(')
{
stk.push(i);
}
else // if (s[i] == ')')
{
// Stack not empty, and symbol matches.
if (!stk.empty() && s[stk.top()]=='(')
{
// Pop matched pairs.
stk.pop();
if (stk.empty())
{
// From Start to i is a valid string.
maxp = std::max(i + 1, maxp);
}
else
{
// From stk.top()+1 to i is a valid string. Length: i-(stk.top()+1)+1
maxp = std::max(i - stk.top(), maxp);
}
}
else
{
// new start point (stub)
stk.push(i);
}
}
}
return maxp;
}
};