-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode032_Longest_Valid_Parentheses.cpp
More file actions
61 lines (52 loc) · 1.26 KB
/
LeetCode032_Longest_Valid_Parentheses.cpp
File metadata and controls
61 lines (52 loc) · 1.26 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
49
50
51
52
53
54
55
56
57
58
59
60
61
class Solution {
public:
int longestValidParentheses(string s) {
int result = 0;
int max = 0;
int sum = 0;
stack<int> tmp;
//先剔除开题")",好像也没有必要
for(int i = 0; i < s.size(); i++)
{
if(s[i] == ')')
{
s.erase(s.begin()+i);
i--;
}
else
{
break;
}
}
//cout << s;
for(int i = 0; i < s.size(); i++)
{
if(s[i] == '(')
{
tmp.push(i);
continue;
}
//分别整合合法子串长度
if(tmp.size() == 0)
{
sum = 0;
continue;
}
result = i - tmp.top() + 1;
tmp.pop();
if(tmp.size() == 0)
{
sum += result;
if(sum > max)
max = sum;
}
else
{
result = i - tmp.top();
if(result > max)
max = result;
}
}
return max;
}
};