forked from walkccc/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0032.py
More file actions
32 lines (28 loc) · 760 Bytes
/
0032.py
File metadata and controls
32 lines (28 loc) · 760 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
class Solution:
def longestValidParentheses(self, s: str) -> int:
ans = 0
left = 0
right = 0
for i in range(len(s)):
if s[i] == '(':
left += 1
else:
right += 1
if left == right:
ans = max(ans, 2 * right)
elif right > left:
left = 0
right = 0
left = 0
right = 0
for i in range(len(s) - 1, -1, -1):
if s[i] == '(':
left += 1
else:
right += 1
if left == right:
ans = max(ans, 2 * left)
elif left > right:
left = 0
right = 0
return ans