mirror of
https://github.com/Kiritow/OJ-Problems-Source.git
synced 2024-03-22 13:11:29 +08:00
Create 32.cpp
This commit is contained in:
parent
843ac0e9bc
commit
498579d77d
43
LeetCode-CN/32.cpp
Normal file
43
LeetCode-CN/32.cpp
Normal 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;
|
||||||
|
}
|
||||||
|
};
|
Loading…
x
Reference in New Issue
Block a user