-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path32.cpp
More file actions
29 lines (28 loc) · 741 Bytes
/
32.cpp
File metadata and controls
29 lines (28 loc) · 741 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
class Solution {
public:
int longestValidParentheses(string s) {
int left, right;
int res = 0;
left = 0;
right = 0;
for (auto c : s) {
if (c == '(') left++;
else right++;
if (left == right) res = max(res, left + right);
if (right > left) {
left = right = 0;
}
}
left = right = 0;
for (int i = s.size() - 1; i >= 0; --i) {
char c = s[i];
if (c == '(') left++;
else right++;
if (left == right) res = max(res, left + right);
if (right < left) {
left = right = 0;
}
}
return res;
}
};