-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlongestvalidparentheses.cpp
More file actions
48 lines (48 loc) · 1.03 KB
/
longestvalidparentheses.cpp
File metadata and controls
48 lines (48 loc) · 1.03 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
class Solution {
public:
int longestValidParentheses(string s) {
int len = s.length();
if (len == 0)
{
return 0;
}
int count = 0;
int maxc = 0;
vector<int>mark(len, 0);
stack<int>ss;
for (int i = 0; i < len; i++)
{
if (s[i] == '(')
{
ss.push(i);
}
else
{
if (ss.empty())
{
continue;
}
else
{
auto nn = ss.top();
mark[nn] = 1;
ss.pop();
mark[i] = 1;
}
}
}
for (auto x : mark)
{
if (x == 1)
{
count++;
maxc = max(maxc, count);
}
else
{
count = 0;
}
}
return maxc;
}
};