forked from moranzcw/LeetCode-NOTES
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.cpp
More file actions
33 lines (33 loc) · 680 Bytes
/
solution.cpp
File metadata and controls
33 lines (33 loc) · 680 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Solution
{
public:
int longestValidParentheses(string s)
{
stack<int>S;
S.push(-1);
int ans=0;
for(string::size_type i=0;i<s.size();i++)
{
char ch=s[i];
if(ch == '(')
{
S.push(i);
}
else
{
if(S.size()>1)
{
S.pop();
int tmp=S.top();
ans=max(ans,(int)i-tmp);
}
else
{
S.pop();
S.push(i);
}
}
}
return ans;
}
};